home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / sun4.md / readline / RCS / readline.c,v < prev   
Encoding:
Text File  |  1992-06-24  |  151.2 KB  |  6,558 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     92.06.09.14.25.12;  author secor;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19. 1.1
  20. log
  21. @Initial revision
  22. @
  23. text
  24. @/* readline.c -- a general facility for reading lines of input
  25.    with emacs style editing and completion. */
  26.  
  27. /* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  28.  
  29.    This file contains the Readline Library (the Library), a set of
  30.    routines for providing Emacs style line input to programs that ask
  31.    for it.
  32.  
  33.    The Library is free software; you can redistribute it and/or modify
  34.    it under the terms of the GNU General Public License as published by
  35.    the Free Software Foundation; either version 2 of the License, or
  36.    (at your option) any later version.
  37.  
  38.    The Library is distributed in the hope that it will be useful,
  39.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  40.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  41.    GNU General Public License for more details.
  42.  
  43.    You should have received a copy of the GNU General Public License
  44.    along with this program; if not, write to the Free Software
  45.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  46.  
  47. /* Remove these declarations when we have a complete libgnu.a. */
  48. /* #define STATIC_MALLOC */
  49. #if !defined (STATIC_MALLOC)
  50. extern char *xmalloc (), *xrealloc ();
  51. #else
  52. static char *xmalloc (), *xrealloc ();
  53. #endif /* STATIC_MALLOC */
  54.  
  55. #include "sysdep.h"
  56. #include <sys/types.h>
  57. #include <stdio.h>
  58. #include <fcntl.h>
  59. #ifndef    NO_SYS_FILE
  60. #include <sys/file.h>
  61. #endif
  62. #include <signal.h>
  63. #include <sys/dir.h>
  64.  
  65. #if defined (HAVE_UNISTD_H)
  66. #  include <unistd.h>
  67. #endif
  68.  
  69. #define NEW_TTY_DRIVER
  70. #define HAVE_BSD_SIGNALS
  71. /* #define USE_XON_XOFF */
  72.  
  73. #ifdef __MSDOS__
  74. #undef NEW_TTY_DRIVER
  75. #undef HAVE_BSD_SIGNALS
  76. #endif
  77.  
  78. /* Some USG machines have BSD signal handling (sigblock, sigsetmask, etc.) */
  79. #if defined (USG) && !defined (hpux)
  80. #undef HAVE_BSD_SIGNALS
  81. #endif
  82.  
  83. /* System V machines use termio. */
  84. #if !defined (_POSIX_VERSION)
  85. #  if defined (USG) || defined (hpux) || defined (Xenix) || defined (sgi) || defined (DGUX)
  86. #    undef NEW_TTY_DRIVER
  87. #    define TERMIO_TTY_DRIVER
  88. #    include <termio.h>
  89. #    if !defined (TCOON)
  90. #      define TCOON 1
  91. #    endif
  92. #  endif /* USG || hpux || Xenix || sgi || DUGX */
  93. #endif /* !_POSIX_VERSION */
  94.  
  95. /* Posix systems use termios and the Posix signal functions. */
  96. #if defined (_POSIX_VERSION)
  97. #  if !defined (TERMIOS_MISSING)
  98. #    undef NEW_TTY_DRIVER
  99. #    define TERMIOS_TTY_DRIVER
  100. #    include <termios.h>
  101. #  endif /* !TERMIOS_MISSING */
  102. #  define HAVE_POSIX_SIGNALS
  103. #  if !defined (O_NDELAY)
  104. #    define O_NDELAY O_NONBLOCK    /* Posix-style non-blocking i/o */
  105. #  endif /* O_NDELAY */
  106. #endif /* _POSIX_VERSION */
  107.  
  108. /* Other (BSD) machines use sgtty. */
  109. #if defined (NEW_TTY_DRIVER)
  110. #include <sgtty.h>
  111. #endif
  112.  
  113. /* Define _POSIX_VDISABLE if we are not using the `new' tty driver and
  114.    it is not already defined.  It is used both to determine if a
  115.    special character is disabled and to disable certain special
  116.    characters.  Posix systems should set to 0, USG systems to -1. */
  117. #if !defined (NEW_TTY_DRIVER) && !defined (_POSIX_VDISABLE)
  118. #  if defined (_POSIX_VERSION)
  119. #    define _POSIX_VDISABLE 0
  120. #  else /* !_POSIX_VERSION */
  121. #    define _POSIX_VDISABLE -1
  122. #  endif /* !_POSIX_VERSION */
  123. #endif /* !NEW_TTY_DRIVER && !_POSIX_VDISABLE */
  124.  
  125. #include <errno.h>
  126. extern int errno;
  127.  
  128. #include <setjmp.h>
  129. #include <sys/stat.h>
  130.  
  131. /* Posix macro to check file in statbuf for directory-ness. */
  132. #if defined (S_IFDIR) && !defined (S_ISDIR)
  133. #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
  134. #endif
  135.  
  136. #ifndef __MSDOS__
  137. /* These next are for filename completion.  Perhaps this belongs
  138.    in a different place. */
  139. #include <pwd.h>
  140. #endif /* __MSDOS__ */
  141.  
  142. #if defined (USG) && !defined (isc386) && !defined (sgi)
  143. struct passwd *getpwuid (), *getpwent ();
  144. #endif
  145.  
  146. /* #define HACK_TERMCAP_MOTION */
  147.  
  148. /* Some standard library routines. */
  149. #include "readline.h"
  150. #include "history.h"
  151.  
  152. #ifndef digit
  153. #define digit(c)  ((c) >= '0' && (c) <= '9')
  154. #endif
  155.  
  156. #ifndef isletter
  157. #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
  158. #endif
  159.  
  160. #ifndef digit_value
  161. #define digit_value(c) ((c) - '0')
  162. #endif
  163.  
  164. #ifndef member
  165. #define member(c, s) ((c) ? index ((s), (c)) : 0)
  166. #endif
  167.  
  168. #ifndef isident
  169. #define isident(c) ((isletter(c) || digit(c) || c == '_'))
  170. #endif
  171.  
  172. #ifndef exchange
  173. #define exchange(x, y) {int temp = x; x = y; y = temp;}
  174. #endif
  175.  
  176. #if !defined (rindex)
  177. extern char *rindex ();
  178. #endif /* rindex */
  179.  
  180. #if !defined (index)
  181. extern char *index ();
  182. #endif /* index */
  183.  
  184. extern char *getenv ();
  185. extern char *tilde_expand ();
  186.  
  187. static update_line ();
  188. static void output_character_function ();
  189. static delete_chars ();
  190. static insert_some_chars ();
  191.  
  192. #if defined (VOID_SIGHANDLER)
  193. #  define sighandler void
  194. #else
  195. #  define sighandler int
  196. #endif /* VOID_SIGHANDLER */
  197.  
  198. /* This typedef is equivalant to the one for Function; it allows us
  199.    to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
  200. typedef sighandler SigHandler ();
  201.  
  202. /* If on, then readline handles signals in a way that doesn't screw. */
  203. #define HANDLE_SIGNALS
  204.  
  205. #ifdef __GO32__
  206. #include <pc.h>
  207. #undef HANDLE_SIGNALS
  208. #endif
  209.  
  210.  
  211. /* **************************************************************** */
  212. /*                                    */
  213. /*            Line editing input utility            */
  214. /*                                    */
  215. /* **************************************************************** */
  216.  
  217. /* A pointer to the keymap that is currently in use.
  218.    By default, it is the standard emacs keymap. */
  219. Keymap keymap = emacs_standard_keymap;
  220.  
  221. #define no_mode -1
  222. #define vi_mode 0
  223. #define emacs_mode 1
  224.  
  225. /* The current style of editing. */
  226. int rl_editing_mode = emacs_mode;
  227.  
  228. /* Non-zero if the previous command was a kill command. */
  229. static int last_command_was_kill = 0;
  230.  
  231. /* The current value of the numeric argument specified by the user. */
  232. int rl_numeric_arg = 1;
  233.  
  234. /* Non-zero if an argument was typed. */
  235. int rl_explicit_arg = 0;
  236.  
  237. /* Temporary value used while generating the argument. */
  238. int rl_arg_sign = 1;
  239.  
  240. /* Non-zero means we have been called at least once before. */
  241. static int rl_initialized = 0;
  242.  
  243. /* If non-zero, this program is running in an EMACS buffer. */
  244. static char *running_in_emacs = (char *)NULL;
  245.  
  246. /* The current offset in the current input line. */
  247. int rl_point;
  248.  
  249. /* Mark in the current input line. */
  250. int rl_mark;
  251.  
  252. /* Length of the current input line. */
  253. int rl_end;
  254.  
  255. /* Make this non-zero to return the current input_line. */
  256. int rl_done;
  257.  
  258. /* The last function executed by readline. */
  259. Function *rl_last_func = (Function *)NULL;
  260.  
  261. /* Top level environment for readline_internal (). */
  262. static jmp_buf readline_top_level;
  263.  
  264. /* The streams we interact with. */
  265. static FILE *in_stream, *out_stream;
  266.  
  267. /* The names of the streams that we do input and output to. */
  268. FILE *rl_instream = stdin, *rl_outstream = stdout;
  269.  
  270. /* Non-zero means echo characters as they are read. */
  271. int readline_echoing_p = 1;
  272.  
  273. /* Current prompt. */
  274. char *rl_prompt;
  275.  
  276. /* The number of characters read in order to type this complete command. */
  277. int rl_key_sequence_length = 0;
  278.  
  279. /* If non-zero, then this is the address of a function to call just
  280.    before readline_internal () prints the first prompt. */
  281. Function *rl_startup_hook = (Function *)NULL;
  282.  
  283. /* If non-zero, then this is the address of a function to call when
  284.    completing on a directory name.  The function is called with
  285.    the address of a string (the current directory name) as an arg. */
  286. Function *rl_symbolic_link_hook = (Function *)NULL;
  287.  
  288. /* What we use internally.  You should always refer to RL_LINE_BUFFER. */
  289. static char *the_line;
  290.  
  291. /* The character that can generate an EOF.  Really read from
  292.    the terminal driver... just defaulted here. */
  293. static int eof_char = CTRL ('D');
  294.  
  295. /* Non-zero makes this the next keystroke to read. */
  296. int rl_pending_input = 0;
  297.  
  298. /* Pointer to a useful terminal name. */
  299. char *rl_terminal_name = (char *)NULL;
  300.  
  301. /* Line buffer and maintenence. */
  302. char *rl_line_buffer = (char *)NULL;
  303. int rl_line_buffer_len = 0;
  304. #define DEFAULT_BUFFER_SIZE 256
  305.  
  306.  
  307. /* **************************************************************** */
  308. /*                                    */
  309. /*            `Forward' declarations              */
  310. /*                                    */
  311. /* **************************************************************** */
  312.  
  313. /* Non-zero means do not parse any lines other than comments and
  314.    parser directives. */
  315. static unsigned char parsing_conditionalized_out = 0;
  316.  
  317. /* Caseless strcmp (). */
  318. static int stricmp (), strnicmp ();
  319.  
  320. /* Non-zero means to save keys that we dispatch on in a kbd macro. */
  321. static int defining_kbd_macro = 0;
  322.  
  323.  
  324. /* **************************************************************** */
  325. /*                                    */
  326. /*            Top Level Functions                */
  327. /*                                    */
  328. /* **************************************************************** */
  329.  
  330. static void rl_prep_terminal (), rl_deprep_terminal ();
  331.  
  332. /* Read a line of input.  Prompt with PROMPT.  A NULL PROMPT means
  333.    none.  A return value of NULL means that EOF was encountered. */
  334. char *
  335. readline (prompt)
  336.      char *prompt;
  337. {
  338.   char *readline_internal ();
  339.   char *value;
  340.  
  341.   rl_prompt = prompt;
  342.  
  343.   /* If we are at EOF return a NULL string. */
  344.   if (rl_pending_input == EOF)
  345.     {
  346.       rl_pending_input = 0;
  347.       return ((char *)NULL);
  348.     }
  349.  
  350.   rl_initialize ();
  351.   rl_prep_terminal ();
  352.  
  353. #if defined (HANDLE_SIGNALS)
  354.   rl_set_signals ();
  355. #endif
  356.  
  357.   value = readline_internal ();
  358.   rl_deprep_terminal ();
  359.  
  360. #if defined (HANDLE_SIGNALS)
  361.   rl_clear_signals ();
  362. #endif
  363.  
  364.   return (value);
  365. }
  366.  
  367. /* Read a line of input from the global rl_instream, doing output on
  368.    the global rl_outstream.
  369.    If rl_prompt is non-null, then that is our prompt. */
  370. char *
  371. readline_internal ()
  372. {
  373.   int lastc, c, eof_found;
  374.  
  375.   in_stream  = rl_instream;
  376.   out_stream = rl_outstream;
  377.  
  378.   lastc = -1;
  379.   eof_found = 0;
  380.  
  381.   if (rl_startup_hook)
  382.     (*rl_startup_hook) ();
  383.  
  384.   if (!readline_echoing_p)
  385.     {
  386.       if (rl_prompt)
  387.     {
  388.       fprintf (out_stream, "%s", rl_prompt);
  389.       fflush (out_stream);
  390.     }
  391.     }
  392.   else
  393.     {
  394.       rl_on_new_line ();
  395.       rl_redisplay ();
  396. #if defined (VI_MODE)
  397.       if (rl_editing_mode == vi_mode)
  398.     rl_vi_insertion_mode ();
  399. #endif /* VI_MODE */
  400.     }
  401.  
  402.   while (!rl_done)
  403.     {
  404.       int lk = last_command_was_kill;
  405.       int code = setjmp (readline_top_level);
  406.  
  407.       if (code)
  408.     rl_redisplay ();
  409.  
  410.       if (!rl_pending_input)
  411.     {
  412.       /* Then initialize the argument and number of keys read. */
  413.       rl_init_argument ();
  414.       rl_key_sequence_length = 0;
  415.     }
  416.  
  417.       c = rl_read_key ();
  418.  
  419.       /* EOF typed to a non-blank line is a <NL>. */
  420.       if (c == EOF && rl_end)
  421.     c = NEWLINE;
  422.  
  423.       /* The character eof_char typed to blank line, and not as the
  424.      previous character is interpreted as EOF. */
  425.       if (((c == eof_char && lastc != c) || c == EOF) && !rl_end)
  426.     {
  427.       eof_found = 1;
  428.       break;
  429.     }
  430.  
  431.       lastc = c;
  432.       rl_dispatch (c, keymap);
  433.  
  434.       /* If there was no change in last_command_was_kill, then no kill
  435.      has taken place.  Note that if input is pending we are reading
  436.      a prefix command, so nothing has changed yet. */
  437.       if (!rl_pending_input)
  438.     {
  439.       if (lk == last_command_was_kill)
  440.         last_command_was_kill = 0;
  441.     }
  442.  
  443. #if defined (VI_MODE)
  444.       /* In vi mode, when you exit insert mode, the cursor moves back
  445.      over the previous character.  We explicitly check for that here. */
  446.       if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap)
  447.     rl_vi_check ();
  448. #endif /* VI_MODE */
  449.  
  450.       if (!rl_done)
  451.     rl_redisplay ();
  452.     }
  453.  
  454.   /* Restore the original of this history line, iff the line that we
  455.      are editing was originally in the history, AND the line has changed. */
  456.   {
  457.     HIST_ENTRY *entry = current_history ();
  458.  
  459.     if (entry && rl_undo_list)
  460.       {
  461.     char *temp = savestring (the_line);
  462.     rl_revert_line ();
  463.     entry = replace_history_entry (where_history (), the_line,
  464.                        (HIST_ENTRY *)NULL);
  465.     free_history_entry (entry);
  466.  
  467.     strcpy (the_line, temp);
  468.     free (temp);
  469.       }
  470.   }
  471.  
  472.   /* At any rate, it is highly likely that this line has an undo list.  Get
  473.      rid of it now. */
  474.   if (rl_undo_list)
  475.     free_undo_list ();
  476.  
  477.   if (eof_found)
  478.     return (char *)NULL;
  479.   else
  480.     return (savestring (the_line));
  481. }
  482.  
  483.  
  484. /* **************************************************************** */
  485. /*                                        */
  486. /*               Signal Handling                          */
  487. /*                                    */
  488. /* **************************************************************** */
  489.  
  490. #if defined (SIGWINCH)
  491. static SigHandler *old_sigwinch = (SigHandler *)NULL;
  492.  
  493. static sighandler
  494. rl_handle_sigwinch (sig)
  495.      int sig;
  496. {
  497.   char *term;
  498.  
  499.   term = rl_terminal_name;
  500.  
  501.   if (readline_echoing_p)
  502.     {
  503.       if (!term)
  504.     term = getenv ("TERM");
  505.       if (!term)
  506.     term = "dumb";
  507.       rl_reset_terminal (term);
  508. #if defined (NOTDEF)
  509.       crlf ();
  510.       rl_forced_update_display ();
  511. #endif /* NOTDEF */
  512.     }
  513.  
  514.   if (old_sigwinch &&
  515.       old_sigwinch != (SigHandler *)SIG_IGN &&
  516.       old_sigwinch != (SigHandler *)SIG_DFL)
  517.     (*old_sigwinch) (sig);
  518. #if !defined (VOID_SIGHANDLER)
  519.   return (0);
  520. #endif /* VOID_SIGHANDLER */
  521. }
  522. #endif  /* SIGWINCH */
  523.  
  524. #if defined (HANDLE_SIGNALS)
  525. /* Interrupt handling. */
  526. static SigHandler
  527.   *old_int  = (SigHandler *)NULL,
  528.   *old_tstp = (SigHandler *)NULL,
  529.   *old_ttou = (SigHandler *)NULL,
  530.   *old_ttin = (SigHandler *)NULL,
  531.   *old_cont = (SigHandler *)NULL,
  532.   *old_alrm = (SigHandler *)NULL;
  533.  
  534. /* Handle an interrupt character. */
  535. static sighandler
  536. rl_signal_handler (sig)
  537.      int sig;
  538. {
  539. #if !defined (HAVE_BSD_SIGNALS)
  540.   /* Since the signal will not be blocked while we are in the signal
  541.      handler, ignore it until rl_clear_signals resets the catcher. */
  542.   if (sig == SIGINT)
  543.     signal (sig, SIG_IGN);
  544. #endif /* !HAVE_BSD_SIGNALS */
  545.  
  546.   switch (sig)
  547.     {
  548.     case SIGINT:
  549.       free_undo_list ();
  550.       rl_clear_message ();
  551.       rl_init_argument ();
  552.  
  553. #if defined (SIGTSTP)
  554.     case SIGTSTP:
  555.     case SIGTTOU:
  556.     case SIGTTIN:
  557. #endif /* SIGTSTP */
  558.     case SIGALRM:
  559.       rl_clean_up_for_exit ();
  560.       rl_deprep_terminal ();
  561.       rl_clear_signals ();
  562.       rl_pending_input = 0;
  563.  
  564.       kill (getpid (), sig);
  565.  
  566. #if defined (HAVE_POSIX_SIGNALS)
  567.       {
  568.     sigset_t set;
  569.  
  570.     sigemptyset (&set);
  571.     sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
  572.       }
  573. #else
  574. #if defined (HAVE_BSD_SIGNALS)
  575.       sigsetmask (0);
  576. #endif /* HAVE_BSD_SIGNALS */
  577. #endif /* HAVE_POSIX_SIGNALS */
  578.  
  579.       rl_prep_terminal ();
  580.       rl_set_signals ();
  581.     }
  582.  
  583. #if !defined (VOID_SIGHANDLER)
  584.   return (0);
  585. #endif /* !VOID_SIGHANDLER */
  586. }
  587.  
  588. rl_set_signals ()
  589. {
  590.   old_int = (SigHandler *)signal (SIGINT, rl_signal_handler);
  591.   if (old_int == (SigHandler *)SIG_IGN)
  592.     signal (SIGINT, SIG_IGN);
  593.  
  594.   old_alrm = (SigHandler *)signal (SIGALRM, rl_signal_handler);
  595.   if (old_alrm == (SigHandler *)SIG_IGN)
  596.     signal (SIGALRM, SIG_IGN);
  597.  
  598. #if defined (SIGTSTP)
  599.   old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler);
  600.   if (old_tstp == (SigHandler *)SIG_IGN)
  601.     signal (SIGTSTP, SIG_IGN);
  602. #endif
  603. #if defined (SIGTTOU)
  604.   old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler);
  605.   old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler);
  606.  
  607.   if (old_tstp == (SigHandler *)SIG_IGN)
  608.     {
  609.       signal (SIGTTOU, SIG_IGN);
  610.       signal (SIGTTIN, SIG_IGN);
  611.     }
  612. #endif
  613.  
  614. #if defined (SIGWINCH)
  615.   old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch);
  616. #endif
  617. }
  618.  
  619. rl_clear_signals ()
  620. {
  621.   signal (SIGINT, old_int);
  622.   signal (SIGALRM, old_alrm);
  623.  
  624. #if defined (SIGTSTP)
  625.   signal (SIGTSTP, old_tstp);
  626. #endif
  627.  
  628. #if defined (SIGTTOU)
  629.   signal (SIGTTOU, old_ttou);
  630.   signal (SIGTTIN, old_ttin);
  631. #endif
  632.  
  633. #if defined (SIGWINCH)
  634.       signal (SIGWINCH, old_sigwinch);
  635. #endif
  636. }
  637. #endif  /* HANDLE_SIGNALS */
  638.  
  639.  
  640. /* **************************************************************** */
  641. /*                                    */
  642. /*            Character Input Buffering               */
  643. /*                                    */
  644. /* **************************************************************** */
  645.  
  646. #if defined (USE_XON_XOFF)
  647. /* If the terminal was in xoff state when we got to it, then xon_char
  648.    contains the character that is supposed to start it again. */
  649. static int xon_char, xoff_state;
  650. #endif /* USE_XON_XOFF */
  651.  
  652. static int pop_index = 0, push_index = 0, ibuffer_len = 511;
  653. static unsigned char ibuffer[512];
  654.  
  655. /* Non-null means it is a pointer to a function to run while waiting for
  656.    character input. */
  657. Function *rl_event_hook = (Function *)NULL;
  658.  
  659. #define any_typein (push_index != pop_index)
  660.  
  661. /* Add KEY to the buffer of characters to be read. */
  662. rl_stuff_char (key)
  663.      int key;
  664. {
  665.   if (key == EOF)
  666.     {
  667.       key = NEWLINE;
  668.       rl_pending_input = EOF;
  669.     }
  670.   ibuffer[push_index++] = key;
  671.   if (push_index >= ibuffer_len)
  672.     push_index = 0;
  673. }
  674.  
  675. /* Return the amount of space available in the
  676.    buffer for stuffing characters. */
  677. int
  678. ibuffer_space ()
  679. {
  680.   if (pop_index > push_index)
  681.     return (pop_index - push_index);
  682.   else
  683.     return (ibuffer_len - (push_index - pop_index));
  684. }
  685.  
  686. /* Get a key from the buffer of characters to be read.
  687.    Return the key in KEY.
  688.    Result is KEY if there was a key, or 0 if there wasn't. */
  689. int
  690. rl_get_char (key)
  691.      int *key;
  692. {
  693.   if (push_index == pop_index)
  694.     return (0);
  695.  
  696.   *key = ibuffer[pop_index++];
  697.  
  698.   if (pop_index >= ibuffer_len)
  699.     pop_index = 0;
  700.  
  701.   return (1);
  702. }
  703.  
  704. /* Stuff KEY into the *front* of the input buffer.
  705.    Returns non-zero if successful, zero if there is
  706.    no space left in the buffer. */
  707. int
  708. rl_unget_char (key)
  709.      int key;
  710. {
  711.   if (ibuffer_space ())
  712.     {
  713.       pop_index--;
  714.       if (pop_index < 0)
  715.     pop_index = ibuffer_len - 1;
  716.       ibuffer[pop_index] = key;
  717.       return (1);
  718.     }
  719.   return (0);
  720. }
  721.  
  722. /* If a character is available to be read, then read it
  723.    and stuff it into IBUFFER.  Otherwise, just return. */
  724. rl_gather_tyi ()
  725. {
  726. #ifdef __GO32__
  727.   char input;
  728.   if (isatty(0))
  729.   {
  730.     int i = rl_getc();
  731.     if (i != EOF)
  732.       rl_stuff_char(i);
  733.   }
  734.   else
  735.     if (kbhit() && ibuffer_space())
  736.       rl_stuff_char(getkey());
  737. #else
  738.   int tty = fileno (in_stream);
  739.   register int tem, result = -1;
  740.   long chars_avail;
  741.   char input;
  742.  
  743. #if defined (FIONREAD)
  744.   result = ioctl (tty, FIONREAD, &chars_avail);
  745. #endif
  746.  
  747.   if (result == -1)
  748.     {
  749.       int flags;
  750.  
  751.       flags = fcntl (tty, F_GETFL, 0);
  752.  
  753.       fcntl (tty, F_SETFL, (flags | O_NDELAY));
  754.       chars_avail = read (tty, &input, 1);
  755.  
  756.       fcntl (tty, F_SETFL, flags);
  757.       if (chars_avail == -1 && errno == EAGAIN)
  758.     return;
  759.     }
  760.  
  761.   /* If there's nothing available, don't waste time trying to read
  762.      something. */
  763.   if (chars_avail == 0)
  764.     return;
  765.  
  766.   tem = ibuffer_space ();
  767.  
  768.   if (chars_avail > tem)
  769.     chars_avail = tem;
  770.  
  771.   /* One cannot read all of the available input.  I can only read a single
  772.      character at a time, or else programs which require input can be
  773.      thwarted.  If the buffer is larger than one character, I lose.
  774.      Damn! */
  775.   if (tem < ibuffer_len)
  776.     chars_avail = 0;
  777.  
  778.   if (result != -1)
  779.     {
  780.       while (chars_avail--)
  781.     rl_stuff_char (rl_getc (in_stream));
  782.     }
  783.   else
  784.     {
  785.       if (chars_avail)
  786.     rl_stuff_char (input);
  787.     }
  788. #endif /* def __GO32__/else */
  789. }
  790.  
  791. static int next_macro_key ();
  792. /* Read a key, including pending input. */
  793. int
  794. rl_read_key ()
  795. {
  796.   int c;
  797.  
  798.   rl_key_sequence_length++;
  799.  
  800.   if (rl_pending_input)
  801.     {
  802.       c = rl_pending_input;
  803.       rl_pending_input = 0;
  804.     }
  805.   else
  806.     {
  807.       /* If input is coming from a macro, then use that. */
  808.       if (c = next_macro_key ())
  809.     return (c);
  810.  
  811.       /* If the user has an event function, then call it periodically. */
  812.       if (rl_event_hook)
  813.     {
  814.       while (rl_event_hook && !rl_get_char (&c))
  815.         {
  816.           (*rl_event_hook) ();
  817.           rl_gather_tyi ();
  818.         }
  819.     }
  820.       else
  821.     {
  822.       if (!rl_get_char (&c))
  823.         c = rl_getc (in_stream);
  824.     }
  825.     }
  826.  
  827.   return (c);
  828. }
  829.  
  830. /* I'm beginning to hate the declaration rules for various compilers. */
  831. static void add_macro_char (), with_macro_input ();
  832.  
  833. /* Do the command associated with KEY in MAP.
  834.    If the associated command is really a keymap, then read
  835.    another key, and dispatch into that map. */
  836. rl_dispatch (key, map)
  837.      register int key;
  838.      Keymap map;
  839. {
  840.  
  841.   if (defining_kbd_macro)
  842.     add_macro_char (key);
  843.  
  844.   if (key > 127 && key < 256)
  845.     {
  846.       if (map[ESC].type == ISKMAP)
  847.     {
  848.       map = (Keymap)map[ESC].function;
  849.       key -= 128;
  850.       rl_dispatch (key, map);
  851.     }
  852.       else
  853.     ding ();
  854.       return;
  855.     }
  856.  
  857.   switch (map[key].type)
  858.     {
  859.     case ISFUNC:
  860.       {
  861.     Function *func = map[key].function;
  862.  
  863.     if (func != (Function *)NULL)
  864.       {
  865.         /* Special case rl_do_lowercase_version (). */
  866.         if (func == rl_do_lowercase_version)
  867.           {
  868.         rl_dispatch (to_lower (key), map);
  869.         return;
  870.           }
  871.  
  872.         (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
  873.  
  874.         /* If we have input pending, then the last command was a prefix
  875.            command.  Don't change the state of rl_last_func.  Otherwise,
  876.            remember the last command executed in this variable. */
  877.         if (!rl_pending_input)
  878.           rl_last_func = map[key].function;
  879.       }
  880.     else
  881.       {
  882.         rl_abort ();
  883.         return;
  884.       }
  885.       }
  886.       break;
  887.  
  888.     case ISKMAP:
  889.       if (map[key].function != (Function *)NULL)
  890.     {
  891.       int newkey;
  892.  
  893.       rl_key_sequence_length++;
  894.       newkey = rl_read_key ();
  895.       rl_dispatch (newkey, (Keymap)map[key].function);
  896.     }
  897.       else
  898.     {
  899.       rl_abort ();
  900.       return;
  901.     }
  902.       break;
  903.  
  904.     case ISMACR:
  905.       if (map[key].function != (Function *)NULL)
  906.     {
  907.       char *macro;
  908.  
  909.       macro = savestring ((char *)map[key].function);
  910.       with_macro_input (macro);
  911.       return;
  912.     }
  913.       break;
  914.     }
  915. }
  916.  
  917.  
  918. /* **************************************************************** */
  919. /*                                    */
  920. /*            Hacking Keyboard Macros             */
  921. /*                                    */
  922. /* **************************************************************** */
  923.  
  924. /* The currently executing macro string.  If this is non-zero,
  925.    then it is a malloc ()'ed string where input is coming from. */
  926. static char *executing_macro = (char *)NULL;
  927.  
  928. /* The offset in the above string to the next character to be read. */
  929. static int executing_macro_index = 0;
  930.  
  931. /* The current macro string being built.  Characters get stuffed
  932.    in here by add_macro_char (). */
  933. static char *current_macro = (char *)NULL;
  934.  
  935. /* The size of the buffer allocated to current_macro. */
  936. static int current_macro_size = 0;
  937.  
  938. /* The index at which characters are being added to current_macro. */
  939. static int current_macro_index = 0;
  940.  
  941. /* A structure used to save nested macro strings.
  942.    It is a linked list of string/index for each saved macro. */
  943. struct saved_macro {
  944.   struct saved_macro *next;
  945.   char *string;
  946.   int index;
  947. };
  948.  
  949. /* The list of saved macros. */
  950. struct saved_macro *macro_list = (struct saved_macro *)NULL;
  951.  
  952. /* Forward declarations of static functions.  Thank you C. */
  953. static void push_executing_macro (), pop_executing_macro ();
  954.  
  955. /* This one has to be declared earlier in the file. */
  956. /* static void add_macro_char (); */
  957.  
  958. /* Set up to read subsequent input from STRING.
  959.    STRING is free ()'ed when we are done with it. */
  960. static void
  961. with_macro_input (string)
  962.      char *string;
  963. {
  964.   push_executing_macro ();
  965.   executing_macro = string;
  966.   executing_macro_index = 0;
  967. }
  968.  
  969. /* Return the next character available from a macro, or 0 if
  970.    there are no macro characters. */
  971. static int
  972. next_macro_key ()
  973. {
  974.   if (!executing_macro)
  975.     return (0);
  976.  
  977.   if (!executing_macro[executing_macro_index])
  978.     {
  979.       pop_executing_macro ();
  980.       return (next_macro_key ());
  981.     }
  982.  
  983.   return (executing_macro[executing_macro_index++]);
  984. }
  985.  
  986. /* Save the currently executing macro on a stack of saved macros. */
  987. static void
  988. push_executing_macro ()
  989. {
  990.   struct saved_macro *saver;
  991.  
  992.   saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
  993.   saver->next = macro_list;
  994.   saver->index = executing_macro_index;
  995.   saver->string = executing_macro;
  996.  
  997.   macro_list = saver;
  998. }
  999.  
  1000. /* Discard the current macro, replacing it with the one
  1001.    on the top of the stack of saved macros. */
  1002. static void
  1003. pop_executing_macro ()
  1004. {
  1005.   if (executing_macro)
  1006.     free (executing_macro);
  1007.  
  1008.   executing_macro = (char *)NULL;
  1009.   executing_macro_index = 0;
  1010.  
  1011.   if (macro_list)
  1012.     {
  1013.       struct saved_macro *disposer = macro_list;
  1014.       executing_macro = macro_list->string;
  1015.       executing_macro_index = macro_list->index;
  1016.       macro_list = macro_list->next;
  1017.       free (disposer);
  1018.     }
  1019. }
  1020.  
  1021. /* Add a character to the macro being built. */
  1022. static void
  1023. add_macro_char (c)
  1024.      int c;
  1025. {
  1026.   if (current_macro_index + 1 >= current_macro_size)
  1027.     {
  1028.       if (!current_macro)
  1029.     current_macro = (char *)xmalloc (current_macro_size = 25);
  1030.       else
  1031.     current_macro =
  1032.       (char *)xrealloc (current_macro, current_macro_size += 25);
  1033.     }
  1034.  
  1035.   current_macro[current_macro_index++] = c;
  1036.   current_macro[current_macro_index] = '\0';
  1037. }
  1038.  
  1039. /* Begin defining a keyboard macro.
  1040.    Keystrokes are recorded as they are executed.
  1041.    End the definition with rl_end_kbd_macro ().
  1042.    If a numeric argument was explicitly typed, then append this
  1043.    definition to the end of the existing macro, and start by
  1044.    re-executing the existing macro. */
  1045. rl_start_kbd_macro (ignore1, ignore2)
  1046.      int ignore1, ignore2;
  1047. {
  1048.   if (defining_kbd_macro)
  1049.     rl_abort ();
  1050.  
  1051.   if (rl_explicit_arg)
  1052.     {
  1053.       if (current_macro)
  1054.     with_macro_input (savestring (current_macro));
  1055.     }
  1056.   else
  1057.     current_macro_index = 0;
  1058.  
  1059.   defining_kbd_macro = 1;
  1060. }
  1061.  
  1062. /* Stop defining a keyboard macro.
  1063.    A numeric argument says to execute the macro right now,
  1064.    that many times, counting the definition as the first time. */
  1065. rl_end_kbd_macro (count, ignore)
  1066.      int count, ignore;
  1067. {
  1068.   if (!defining_kbd_macro)
  1069.     rl_abort ();
  1070.  
  1071.   current_macro_index -= (rl_key_sequence_length - 1);
  1072.   current_macro[current_macro_index] = '\0';
  1073.  
  1074.   defining_kbd_macro = 0;
  1075.  
  1076.   rl_call_last_kbd_macro (--count, 0);
  1077. }
  1078.  
  1079. /* Execute the most recently defined keyboard macro.
  1080.    COUNT says how many times to execute it. */
  1081. rl_call_last_kbd_macro (count, ignore)
  1082.      int count, ignore;
  1083. {
  1084.   if (!current_macro)
  1085.     rl_abort ();
  1086.  
  1087.   while (count--)
  1088.     with_macro_input (savestring (current_macro));
  1089. }
  1090.  
  1091.  
  1092. /* **************************************************************** */
  1093. /*                                    */
  1094. /*            Initializations                 */
  1095. /*                                    */
  1096. /* **************************************************************** */
  1097.  
  1098. /* Initliaze readline (and terminal if not already). */
  1099. rl_initialize ()
  1100. {
  1101.   extern char *rl_display_prompt;
  1102.  
  1103.   /* If we have never been called before, initialize the
  1104.      terminal and data structures. */
  1105.   if (!rl_initialized)
  1106.     {
  1107.       readline_initialize_everything ();
  1108.       rl_initialized++;
  1109.     }
  1110.  
  1111.   /* Initalize the current line information. */
  1112.   rl_point = rl_end = 0;
  1113.   the_line = rl_line_buffer;
  1114.   the_line[0] = 0;
  1115.  
  1116.   /* We aren't done yet.  We haven't even gotten started yet! */
  1117.   rl_done = 0;
  1118.  
  1119.   /* Tell the history routines what is going on. */
  1120.   start_using_history ();
  1121.  
  1122.   /* Make the display buffer match the state of the line. */
  1123.   {
  1124.     extern char *rl_display_prompt;
  1125.     extern int forced_display;
  1126.  
  1127.     rl_on_new_line ();
  1128.  
  1129.     rl_display_prompt = rl_prompt ? rl_prompt : "";
  1130.     forced_display = 1;
  1131.   }
  1132.  
  1133.   /* No such function typed yet. */
  1134.   rl_last_func = (Function *)NULL;
  1135.  
  1136.   /* Parsing of key-bindings begins in an enabled state. */
  1137.   parsing_conditionalized_out = 0;
  1138. }
  1139.  
  1140. /* Initialize the entire state of the world. */
  1141. readline_initialize_everything ()
  1142. {
  1143.   /* Find out if we are running in Emacs. */
  1144.   running_in_emacs = getenv ("EMACS");
  1145.  
  1146.   /* Allocate data structures. */
  1147.   if (!rl_line_buffer)
  1148.     rl_line_buffer =
  1149.       (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
  1150.  
  1151.   /* Initialize the terminal interface. */
  1152.   init_terminal_io ((char *)NULL);
  1153.  
  1154.   /* Bind tty characters to readline functions. */
  1155.   readline_default_bindings ();
  1156.  
  1157.   /* Initialize the function names. */
  1158.   rl_initialize_funmap ();
  1159.  
  1160.   /* Read in the init file. */
  1161.   rl_read_init_file ((char *)NULL);
  1162.  
  1163.   /* If the completion parser's default word break characters haven't
  1164.      been set yet, then do so now. */
  1165.   {
  1166.     extern char *rl_completer_word_break_characters;
  1167.     extern char *rl_basic_word_break_characters;
  1168.  
  1169.     if (rl_completer_word_break_characters == (char *)NULL)
  1170.       rl_completer_word_break_characters = rl_basic_word_break_characters;
  1171.   }
  1172. }
  1173.  
  1174. /* If this system allows us to look at the values of the regular
  1175.    input editing characters, then bind them to their readline
  1176.    equivalents, iff the characters are not bound to keymaps. */
  1177. readline_default_bindings ()
  1178. {
  1179. #ifndef __GO32__
  1180.  
  1181. #if defined (NEW_TTY_DRIVER)
  1182.   struct sgttyb ttybuff;
  1183.   int tty = fileno (rl_instream);
  1184.  
  1185.   if (ioctl (tty, TIOCGETP, &ttybuff) != -1)
  1186.     {
  1187.       int erase, kill;
  1188.  
  1189.       erase = ttybuff.sg_erase;
  1190.       kill  = ttybuff.sg_kill;
  1191.  
  1192.       if (erase != -1 && keymap[erase].type == ISFUNC)
  1193.     keymap[erase].function = rl_rubout;
  1194.  
  1195.       if (kill != -1 && keymap[kill].type == ISFUNC)
  1196.     keymap[kill].function = rl_unix_line_discard;
  1197.     }
  1198.  
  1199. #if defined (TIOCGLTC)
  1200.   {
  1201.     struct ltchars lt;
  1202.  
  1203.     if (ioctl (tty, TIOCGLTC, <) != -1)
  1204.       {
  1205.     int erase, nextc;
  1206.  
  1207.     erase = lt.t_werasc;
  1208.     nextc = lt.t_lnextc;
  1209.  
  1210.     if (erase != -1 && keymap[erase].type == ISFUNC)
  1211.       keymap[erase].function = rl_unix_word_rubout;
  1212.  
  1213.     if (nextc != -1 && keymap[nextc].type == ISFUNC)
  1214.       keymap[nextc].function = rl_quoted_insert;
  1215.       }
  1216.   }
  1217. #endif /* TIOCGLTC */
  1218. #else /* not NEW_TTY_DRIVER */
  1219.  
  1220. #if defined (TERMIOS_TTY_DRIVER)
  1221.   struct termios ttybuff;
  1222. #else
  1223.   struct termio ttybuff;
  1224. #endif /* TERMIOS_TTY_DRIVER */
  1225.   int tty = fileno (rl_instream);
  1226.  
  1227. #if defined (TERMIOS_TTY_DRIVER)
  1228.   if (tcgetattr (tty, &ttybuff) != -1)
  1229. #else
  1230.   if (ioctl (tty, TCGETA, &ttybuff) != -1)
  1231. #endif /* !TERMIOS_TTY_DRIVER */
  1232.     {
  1233.       int erase, kill;
  1234.  
  1235.       erase = ttybuff.c_cc[VERASE];
  1236.       kill = ttybuff.c_cc[VKILL];
  1237.  
  1238.       if (erase != _POSIX_VDISABLE &&
  1239.       keymap[(unsigned char)erase].type == ISFUNC)
  1240.     keymap[(unsigned char)erase].function = rl_rubout;
  1241.  
  1242.       if (kill != _POSIX_VDISABLE &&
  1243.       keymap[(unsigned char)kill].type == ISFUNC)
  1244.     keymap[(unsigned char)kill].function = rl_unix_line_discard;
  1245.  
  1246. #if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
  1247.       {
  1248.     int nextc;
  1249.  
  1250.     nextc = ttybuff.c_cc[VLNEXT];
  1251.  
  1252.     if (nextc != _POSIX_VDISABLE &&
  1253.         keymap[(unsigned char)nextc].type == ISFUNC)
  1254.       keymap[(unsigned char)nextc].function = rl_quoted_insert;
  1255.       }
  1256. #endif /* VLNEXT && TERMIOS_TTY_DRIVER */
  1257.  
  1258. #if defined (VWERASE)
  1259.       {
  1260.     int werase;
  1261.  
  1262.     werase = ttybuff.c_cc[VWERASE];
  1263.  
  1264.     if (werase != _POSIX_VDISABLE &&
  1265.         keymap[(unsigned char)werase].type == ISFUNC)
  1266.       keymap[(unsigned char)werase].function = rl_unix_word_rubout;
  1267.       }
  1268. #endif /* VWERASE */
  1269.     }
  1270. #endif /* !NEW_TTY_DRIVER */
  1271. #endif /* def __GO32__ */
  1272. }
  1273.  
  1274.  
  1275. /* **************************************************************** */
  1276. /*                                    */
  1277. /*            Numeric Arguments                */
  1278. /*                                    */
  1279. /* **************************************************************** */
  1280.  
  1281. /* Handle C-u style numeric args, as well as M--, and M-digits. */
  1282.  
  1283. /* Add the current digit to the argument in progress. */
  1284. rl_digit_argument (ignore, key)
  1285.      int ignore, key;
  1286. {
  1287.   rl_pending_input = key;
  1288.   rl_digit_loop ();
  1289. }
  1290.  
  1291. /* What to do when you abort reading an argument. */
  1292. rl_discard_argument ()
  1293. {
  1294.   ding ();
  1295.   rl_clear_message ();
  1296.   rl_init_argument ();
  1297. }
  1298.  
  1299. /* Create a default argument. */
  1300. rl_init_argument ()
  1301. {
  1302.   rl_numeric_arg = rl_arg_sign = 1;
  1303.   rl_explicit_arg = 0;
  1304. }
  1305.  
  1306. /* C-u, universal argument.  Multiply the current argument by 4.
  1307.    Read a key.  If the key has nothing to do with arguments, then
  1308.    dispatch on it.  If the key is the abort character then abort. */
  1309. rl_universal_argument ()
  1310. {
  1311.   rl_numeric_arg *= 4;
  1312.   rl_digit_loop ();
  1313. }
  1314.  
  1315. rl_digit_loop ()
  1316. {
  1317.   int key, c;
  1318.   while (1)
  1319.     {
  1320.       rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
  1321.       key = c = rl_read_key ();
  1322.  
  1323.       if (keymap[c].type == ISFUNC &&
  1324.       keymap[c].function == rl_universal_argument)
  1325.     {
  1326.       rl_numeric_arg *= 4;
  1327.       continue;
  1328.     }
  1329.       c = UNMETA (c);
  1330.       if (numeric (c))
  1331.     {
  1332.       if (rl_explicit_arg)
  1333.         rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
  1334.       else
  1335.         rl_numeric_arg = (c - '0');
  1336.       rl_explicit_arg = 1;
  1337.     }
  1338.       else
  1339.     {
  1340.       if (c == '-' && !rl_explicit_arg)
  1341.         {
  1342.           rl_numeric_arg = 1;
  1343.           rl_arg_sign = -1;
  1344.         }
  1345.       else
  1346.         {
  1347.           rl_clear_message ();
  1348.           rl_dispatch (key, keymap);
  1349.           return;
  1350.         }
  1351.     }
  1352.     }
  1353. }
  1354.  
  1355.  
  1356. /* **************************************************************** */
  1357. /*                                    */
  1358. /*            Display stuff                    */
  1359. /*                                    */
  1360. /* **************************************************************** */
  1361.  
  1362. /* This is the stuff that is hard for me.  I never seem to write good
  1363.    display routines in C.  Let's see how I do this time. */
  1364.  
  1365. /* (PWP) Well... Good for a simple line updater, but totally ignores
  1366.    the problems of input lines longer than the screen width.
  1367.  
  1368.    update_line and the code that calls it makes a multiple line,
  1369.    automatically wrapping line update.  Carefull attention needs
  1370.    to be paid to the vertical position variables.
  1371.  
  1372.    handling of terminals with autowrap on (incl. DEC braindamage)
  1373.    could be improved a bit.  Right now I just cheat and decrement
  1374.    screenwidth by one. */
  1375.  
  1376. /* Keep two buffers; one which reflects the current contents of the
  1377.    screen, and the other to draw what we think the new contents should
  1378.    be.  Then compare the buffers, and make whatever changes to the
  1379.    screen itself that we should.  Finally, make the buffer that we
  1380.    just drew into be the one which reflects the current contents of the
  1381.    screen, and place the cursor where it belongs.
  1382.  
  1383.    Commands that want to can fix the display themselves, and then let
  1384.    this function know that the display has been fixed by setting the
  1385.    RL_DISPLAY_FIXED variable.  This is good for efficiency. */
  1386.  
  1387. /* Termcap variables: */
  1388. extern char *term_up, *term_dc, *term_cr;
  1389. extern int screenheight, screenwidth, terminal_can_insert;
  1390.  
  1391. /* What YOU turn on when you have handled all redisplay yourself. */
  1392. int rl_display_fixed = 0;
  1393.  
  1394. /* The visible cursor position.  If you print some text, adjust this. */
  1395. int last_c_pos = 0;
  1396. int last_v_pos = 0;
  1397.  
  1398. /* The last left edge of text that was displayed.  This is used when
  1399.    doing horizontal scrolling.  It shifts in thirds of a screenwidth. */
  1400. static int last_lmargin = 0;
  1401.  
  1402. /* The line display buffers.  One is the line currently displayed on
  1403.    the screen.  The other is the line about to be displayed. */
  1404. static char *visible_line = (char *)NULL;
  1405. static char *invisible_line = (char *)NULL;
  1406.  
  1407. /* Number of lines currently on screen minus 1. */
  1408. int vis_botlin = 0;
  1409.  
  1410. /* A buffer for `modeline' messages. */
  1411. char msg_buf[128];
  1412.  
  1413. /* Non-zero forces the redisplay even if we thought it was unnecessary. */
  1414. int forced_display = 0;
  1415.  
  1416. /* The stuff that gets printed out before the actual text of the line.
  1417.    This is usually pointing to rl_prompt. */
  1418. char *rl_display_prompt = (char *)NULL;
  1419.  
  1420. /* Default and initial buffer size.  Can grow. */
  1421. static int line_size = 1024;
  1422.  
  1423. /* Non-zero means to always use horizontal scrolling in line display. */
  1424. static int horizontal_scroll_mode = 0;
  1425.  
  1426. /* Non-zero means to display an asterisk at the starts of history lines
  1427.    which have been modified. */
  1428. static int mark_modified_lines = 0;
  1429.  
  1430. /* Non-zero means to use a visible bell if one is available rather than
  1431.    simply ringing the terminal bell. */
  1432. static int prefer_visible_bell = 0;
  1433.  
  1434. /* I really disagree with this, but my boss (among others) insists that we
  1435.    support compilers that don't work.  I don't think we are gaining by doing
  1436.    so; what is the advantage in producing better code if we can't use it? */
  1437. /* The following two declarations belong inside the
  1438.    function block, not here. */
  1439. static void move_cursor_relative ();
  1440. static void output_some_chars ();
  1441. static void output_character_function ();
  1442. static int compare_strings ();
  1443.  
  1444. /* Basic redisplay algorithm. */
  1445. rl_redisplay ()
  1446. {
  1447.   register int in, out, c, linenum;
  1448.   register char *line = invisible_line;
  1449.   char *prompt_this_line;
  1450.   int c_pos = 0;
  1451.   int inv_botlin = 0;        /* Number of lines in newly drawn buffer. */
  1452.  
  1453.   extern int readline_echoing_p;
  1454.  
  1455.   if (!readline_echoing_p)
  1456.     return;
  1457.  
  1458.   if (!rl_display_prompt)
  1459.     rl_display_prompt = "";
  1460.  
  1461.   if (!invisible_line)
  1462.     {
  1463.       visible_line = (char *)xmalloc (line_size);
  1464.       invisible_line = (char *)xmalloc (line_size);
  1465.       line = invisible_line;
  1466.       for (in = 0; in < line_size; in++)
  1467.     {
  1468.       visible_line[in] = 0;
  1469.       invisible_line[in] = 1;
  1470.     }
  1471.       rl_on_new_line ();
  1472.     }
  1473.  
  1474.   /* Draw the line into the buffer. */
  1475.   c_pos = -1;
  1476.  
  1477.   /* Mark the line as modified or not.  We only do this for history
  1478.      lines. */
  1479.   out = 0;
  1480.   if (mark_modified_lines && current_history () && rl_undo_list)
  1481.     {
  1482.       line[out++] = '*';
  1483.       line[out] = '\0';
  1484.     }
  1485.  
  1486.   /* If someone thought that the redisplay was handled, but the currently
  1487.      visible line has a different modification state than the one about
  1488.      to become visible, then correct the callers misconception. */
  1489.   if (visible_line[0] != invisible_line[0])
  1490.     rl_display_fixed = 0;
  1491.  
  1492.   prompt_this_line = rindex (rl_display_prompt, '\n');
  1493.   if (!prompt_this_line)
  1494.     prompt_this_line = rl_display_prompt;
  1495.   else
  1496.     {
  1497.       prompt_this_line++;
  1498.       if (forced_display)
  1499.     output_some_chars (rl_display_prompt,
  1500.                prompt_this_line - rl_display_prompt);
  1501.     }
  1502.  
  1503.   strncpy (line + out,  prompt_this_line, strlen (prompt_this_line));
  1504.   out += strlen (prompt_this_line);
  1505.   line[out] = '\0';
  1506.  
  1507.   for (in = 0; in < rl_end; in++)
  1508.     {
  1509.       c = (unsigned char)the_line[in];
  1510.  
  1511.       if (out + 1 >= line_size)
  1512.     {
  1513.       line_size *= 2;
  1514.       visible_line = (char *)xrealloc (visible_line, line_size);
  1515.       invisible_line = (char *)xrealloc (invisible_line, line_size);
  1516.       line = invisible_line;
  1517.     }
  1518.  
  1519.       if (in == rl_point)
  1520.     c_pos = out;
  1521.  
  1522.       if (c > 127)
  1523.     {
  1524.       line[out++] = 'M';
  1525.       line[out++] = '-';
  1526.       line[out++] = c - 128;
  1527.     }
  1528. #define DISPLAY_TABS
  1529. #if defined (DISPLAY_TABS)
  1530.       else if (c == '\t')
  1531.     {
  1532.       register int newout = (out | (int)7) + 1;
  1533.       while (out < newout)
  1534.         line[out++] = ' ';
  1535.     }
  1536. #endif
  1537.       else if (c < 32)
  1538.     {
  1539.       line[out++] = 'C';
  1540.       line[out++] = '-';
  1541.       line[out++] = c + 64;
  1542.     }
  1543.       else if (c == 127)
  1544.     {
  1545.       line[out++] = 'C';
  1546.       line[out++] = '-';
  1547.       line[out++] = '?';
  1548.     }
  1549.       else
  1550.     line[out++] = c;
  1551.     }
  1552.   line[out] = '\0';
  1553.   if (c_pos < 0)
  1554.     c_pos = out;
  1555.  
  1556.   /* PWP: now is when things get a bit hairy.  The visible and invisible
  1557.      line buffers are really multiple lines, which would wrap every
  1558.      (screenwidth - 1) characters.  Go through each in turn, finding
  1559.      the changed region and updating it.  The line order is top to bottom. */
  1560.  
  1561.   /* If we can move the cursor up and down, then use multiple lines,
  1562.      otherwise, let long lines display in a single terminal line, and
  1563.      horizontally scroll it. */
  1564.  
  1565.   if (!horizontal_scroll_mode && term_up && *term_up)
  1566.     {
  1567.       int total_screen_chars = (screenwidth * screenheight);
  1568.  
  1569.       if (!rl_display_fixed || forced_display)
  1570.     {
  1571.       forced_display = 0;
  1572.  
  1573.       /* If we have more than a screenful of material to display, then
  1574.          only display a screenful.  We should display the last screen,
  1575.          not the first.  I'll fix this in a minute. */
  1576.       if (out >= total_screen_chars)
  1577.         out = total_screen_chars - 1;
  1578.  
  1579.       /* Number of screen lines to display. */
  1580.       inv_botlin = out / screenwidth;
  1581.  
  1582.       /* For each line in the buffer, do the updating display. */
  1583.       for (linenum = 0; linenum <= inv_botlin; linenum++)
  1584.         update_line (linenum > vis_botlin ? ""
  1585.              : &visible_line[linenum * screenwidth],
  1586.              &invisible_line[linenum * screenwidth],
  1587.              linenum);
  1588.  
  1589.       /* We may have deleted some lines.  If so, clear the left over
  1590.          blank ones at the bottom out. */
  1591.       if (vis_botlin > inv_botlin)
  1592.         {
  1593.           char *tt;
  1594.           for (; linenum <= vis_botlin; linenum++)
  1595.         {
  1596.           tt = &visible_line[linenum * screenwidth];
  1597.           move_vert (linenum);
  1598.           move_cursor_relative (0, tt);
  1599.           clear_to_eol ((linenum == vis_botlin)?
  1600.                 strlen (tt) : screenwidth);
  1601.         }
  1602.         }
  1603.       vis_botlin = inv_botlin;
  1604.  
  1605.       /* Move the cursor where it should be. */
  1606.       move_vert (c_pos / screenwidth);
  1607.       move_cursor_relative (c_pos % screenwidth,
  1608.                 &invisible_line[(c_pos / screenwidth) * screenwidth]);
  1609.     }
  1610.     }
  1611.   else                /* Do horizontal scrolling. */
  1612.     {
  1613.       int lmargin;
  1614.  
  1615.       /* Always at top line. */
  1616.       last_v_pos = 0;
  1617.  
  1618.       /* If the display position of the cursor would be off the edge
  1619.      of the screen, start the display of this line at an offset that
  1620.      leaves the cursor on the screen. */
  1621.       if (c_pos - last_lmargin > screenwidth - 2)
  1622.     lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3);
  1623.       else if (c_pos - last_lmargin < 1)
  1624.     lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3);
  1625.       else
  1626.     lmargin = last_lmargin;
  1627.  
  1628.       /* If the first character on the screen isn't the first character
  1629.      in the display line, indicate this with a special character. */
  1630.       if (lmargin > 0)
  1631.     line[lmargin] = '<';
  1632.  
  1633.       if (lmargin + screenwidth < out)
  1634.     line[lmargin + screenwidth - 1] = '>';
  1635.  
  1636.       if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
  1637.     {
  1638.       forced_display = 0;
  1639.       update_line (&visible_line[last_lmargin],
  1640.                &invisible_line[lmargin], 0);
  1641.  
  1642.       move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
  1643.       last_lmargin = lmargin;
  1644.     }
  1645.     }
  1646.   fflush (out_stream);
  1647.  
  1648.   /* Swap visible and non-visible lines. */
  1649.   {
  1650.     char *temp = visible_line;
  1651.     visible_line = invisible_line;
  1652.     invisible_line = temp;
  1653.     rl_display_fixed = 0;
  1654.   }
  1655. }
  1656.  
  1657. /* PWP: update_line() is based on finding the middle difference of each
  1658.    line on the screen; vis:
  1659.  
  1660.                  /old first difference
  1661.     /beginning of line   |              /old last same       /old EOL
  1662.     v             v              v                    v
  1663. old:    eddie> Oh, my little gruntle-buggy is to me, as lurgid as
  1664. new:    eddie> Oh, my little buggy says to me, as lurgid as
  1665.     ^             ^        ^               ^
  1666.     \beginning of line   |        \new last same       \new end of line
  1667.                  \new first difference
  1668.  
  1669.    All are character pointers for the sake of speed.  Special cases for
  1670.    no differences, as well as for end of line additions must be handeled.
  1671.  
  1672.    Could be made even smarter, but this works well enough */
  1673. static
  1674. update_line (old, new, current_line)
  1675.      register char *old, *new;
  1676.      int current_line;
  1677. {
  1678.   register char *ofd, *ols, *oe, *nfd, *nls, *ne;
  1679.   int lendiff, wsatend;
  1680.  
  1681.   /* Find first difference. */
  1682.   for (ofd = old, nfd = new;
  1683.        (ofd - old < screenwidth) && *ofd && (*ofd == *nfd);
  1684.        ofd++, nfd++)
  1685.     ;
  1686.  
  1687.   /* Move to the end of the screen line. */
  1688.   for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++);
  1689.   for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++);
  1690.  
  1691.   /* If no difference, continue to next line. */
  1692.   if (ofd == oe && nfd == ne)
  1693.     return;
  1694.  
  1695.   wsatend = 1;            /* flag for trailing whitespace */
  1696.   ols = oe - 1;            /* find last same */
  1697.   nls = ne - 1;
  1698.   while ((*ols == *nls) && (ols > ofd) && (nls > nfd))
  1699.     {
  1700.       if (*ols != ' ')
  1701.     wsatend = 0;
  1702.       ols--;
  1703.       nls--;
  1704.     }
  1705.  
  1706.   if (wsatend)
  1707.     {
  1708.       ols = oe;
  1709.       nls = ne;
  1710.     }
  1711.   else if (*ols != *nls)
  1712.     {
  1713.       if (*ols)            /* don't step past the NUL */
  1714.     ols++;
  1715.       if (*nls)
  1716.     nls++;
  1717.     }
  1718.  
  1719.   move_vert (current_line);
  1720.   move_cursor_relative (ofd - old, old);
  1721.  
  1722.   /* if (len (new) > len (old)) */
  1723.   lendiff = (nls - nfd) - (ols - ofd);
  1724.  
  1725.   /* Insert (diff(len(old),len(new)) ch */
  1726.   if (lendiff > 0)
  1727.     {
  1728.       if (terminal_can_insert)
  1729.     {
  1730.       extern char *term_IC;
  1731.  
  1732.       /* Sometimes it is cheaper to print the characters rather than
  1733.          use the terminal's capabilities. */
  1734.       if ((2 * (ne - nfd)) < lendiff && !term_IC)
  1735.         {
  1736.           output_some_chars (nfd, (ne - nfd));
  1737.           last_c_pos += (ne - nfd);
  1738.         }
  1739.       else
  1740.         {
  1741.           if (*ols)
  1742.         {
  1743.           insert_some_chars (nfd, lendiff);
  1744.           last_c_pos += lendiff;
  1745.         }
  1746.           else
  1747.         {
  1748.           /* At the end of a line the characters do not have to
  1749.              be "inserted".  They can just be placed on the screen. */
  1750.           output_some_chars (nfd, lendiff);
  1751.           last_c_pos += lendiff;
  1752.         }
  1753.           /* Copy (new) chars to screen from first diff to last match. */
  1754.           if (((nls - nfd) - lendiff) > 0)
  1755.         {
  1756.           output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff));
  1757.           last_c_pos += ((nls - nfd) - lendiff);
  1758.         }
  1759.         }
  1760.     }
  1761.       else
  1762.     {        /* cannot insert chars, write to EOL */
  1763.       output_some_chars (nfd, (ne - nfd));
  1764.       last_c_pos += (ne - nfd);
  1765.     }
  1766.     }
  1767.   else                /* Delete characters from line. */
  1768.     {
  1769.       /* If possible and inexpensive to use terminal deletion, then do so. */
  1770.       if (term_dc && (2 * (ne - nfd)) >= (-lendiff))
  1771.     {
  1772.       if (lendiff)
  1773.         delete_chars (-lendiff); /* delete (diff) characters */
  1774.  
  1775.       /* Copy (new) chars to screen from first diff to last match */
  1776.       if ((nls - nfd) > 0)
  1777.         {
  1778.           output_some_chars (nfd, (nls - nfd));
  1779.           last_c_pos += (nls - nfd);
  1780.         }
  1781.     }
  1782.       /* Otherwise, print over the existing material. */
  1783.       else
  1784.     {
  1785.       output_some_chars (nfd, (ne - nfd));
  1786.       last_c_pos += (ne - nfd);
  1787.       clear_to_eol ((oe - old) - (ne - new));
  1788.     }
  1789.     }
  1790. }
  1791.  
  1792. /* (PWP) tell the update routines that we have moved onto a
  1793.    new (empty) line. */
  1794. rl_on_new_line ()
  1795. {
  1796.   if (visible_line)
  1797.     visible_line[0] = '\0';
  1798.  
  1799.   last_c_pos = last_v_pos = 0;
  1800.   vis_botlin = last_lmargin = 0;
  1801. }
  1802.  
  1803. /* Actually update the display, period. */
  1804. rl_forced_update_display ()
  1805. {
  1806.   if (visible_line)
  1807.     {
  1808.       register char *temp = visible_line;
  1809.  
  1810.       while (*temp) *temp++ = '\0';
  1811.     }
  1812.   rl_on_new_line ();
  1813.   forced_display++;
  1814.   rl_redisplay ();
  1815. }
  1816.  
  1817. /* Move the cursor from last_c_pos to NEW, which are buffer indices.
  1818.    DATA is the contents of the screen line of interest; i.e., where
  1819.    the movement is being done. */
  1820. static void
  1821. move_cursor_relative (new, data)
  1822.      int new;
  1823.      char *data;
  1824. {
  1825.   register int i;
  1826.  
  1827.   /* It may be faster to output a CR, and then move forwards instead
  1828.      of moving backwards. */
  1829.   if (new + 1 < last_c_pos - new)
  1830.     {
  1831. #ifdef __MSDOS__
  1832.       putc('\r', out_stream);
  1833. #else
  1834.       tputs (term_cr, 1, output_character_function);
  1835. #endif
  1836.       last_c_pos = 0;
  1837.     }
  1838.  
  1839.   if (last_c_pos == new) return;
  1840.  
  1841.   if (last_c_pos < new)
  1842.     {
  1843.       /* Move the cursor forward.  We do it by printing the command
  1844.      to move the cursor forward if there is one, else print that
  1845.      portion of the output buffer again.  Which is cheaper? */
  1846.  
  1847.       /* The above comment is left here for posterity.  It is faster
  1848.      to print one character (non-control) than to print a control
  1849.      sequence telling the terminal to move forward one character.
  1850.      That kind of control is for people who don't know what the
  1851.      data is underneath the cursor. */
  1852. #if defined (HACK_TERMCAP_MOTION)
  1853.       extern char *term_forward_char;
  1854.  
  1855.       if (term_forward_char)
  1856.     for (i = last_c_pos; i < new; i++)
  1857.       tputs (term_forward_char, 1, output_character_function);
  1858.       else
  1859.     for (i = last_c_pos; i < new; i++)
  1860.       putc (data[i], out_stream);
  1861. #else
  1862.       for (i = last_c_pos; i < new; i++)
  1863.     putc (data[i], out_stream);
  1864. #endif                /* HACK_TERMCAP_MOTION */
  1865.     }
  1866.   else
  1867.     backspace (last_c_pos - new);
  1868.   last_c_pos = new;
  1869. }
  1870.  
  1871. /* PWP: move the cursor up or down. */
  1872. move_vert (to)
  1873.      int to;
  1874. {
  1875.   void output_character_function ();
  1876.   register int delta, i;
  1877.  
  1878.   if (last_v_pos == to) return;
  1879.  
  1880.   if (to > screenheight)
  1881.     return;
  1882.  
  1883. #ifdef __GO32__
  1884.   {
  1885.     int cur_r, cur_c;
  1886.     ScreenGetCursor(&cur_r, &cur_c);
  1887.     ScreenSetCursor(cur_r+to-last_v_pos, cur_c);
  1888.   }
  1889. #else /* __GO32__ */
  1890.   if ((delta = to - last_v_pos) > 0)
  1891.     {
  1892.       for (i = 0; i < delta; i++)
  1893.     putc ('\n', out_stream);
  1894.       tputs (term_cr, 1, output_character_function);
  1895.       last_c_pos = 0;
  1896.     }
  1897.   else
  1898.     {            /* delta < 0 */
  1899.       if (term_up && *term_up)
  1900.     for (i = 0; i < -delta; i++)
  1901.       tputs (term_up, 1, output_character_function);
  1902.     }
  1903. #endif /* __GO32__ */
  1904.   last_v_pos = to;        /* now to is here */
  1905. }
  1906.  
  1907. /* Physically print C on out_stream.  This is for functions which know
  1908.    how to optimize the display. */
  1909. rl_show_char (c)
  1910.      int c;
  1911. {
  1912.   if (c > 127)
  1913.     {
  1914.       fprintf (out_stream, "M-");
  1915.       c -= 128;
  1916.     }
  1917.  
  1918. #if defined (DISPLAY_TABS)
  1919.   if (c < 32 && c != '\t')
  1920. #else
  1921.   if (c < 32)
  1922. #endif
  1923.     {
  1924.  
  1925.       c += 64;
  1926.     }
  1927.  
  1928.   putc (c, out_stream);
  1929.   fflush (out_stream);
  1930. }
  1931.  
  1932. #if defined (DISPLAY_TABS)
  1933. int
  1934. rl_character_len (c, pos)
  1935.      register int c, pos;
  1936. {
  1937.   if (c < ' ' || c > 126)
  1938.     {
  1939.       if (c == '\t')
  1940.     return (((pos | (int)7) + 1) - pos);
  1941.       else
  1942.     return (3);
  1943.     }
  1944.   else
  1945.     return (1);
  1946. }
  1947. #else
  1948. int
  1949. rl_character_len (c)
  1950.      int c;
  1951. {
  1952.   if (c < ' ' || c > 126)
  1953.     return (3);
  1954.   else
  1955.     return (1);
  1956. }
  1957. #endif  /* DISPLAY_TAB */
  1958.  
  1959. /* How to print things in the "echo-area".  The prompt is treated as a
  1960.    mini-modeline. */
  1961. rl_message (string, arg1, arg2)
  1962.      char *string;
  1963. {
  1964.   sprintf (msg_buf, string, arg1, arg2);
  1965.   rl_display_prompt = msg_buf;
  1966.   rl_redisplay ();
  1967. }
  1968.  
  1969. /* How to clear things from the "echo-area". */
  1970. rl_clear_message ()
  1971. {
  1972.   rl_display_prompt = rl_prompt;
  1973.   rl_redisplay ();
  1974. }
  1975.  
  1976. /* **************************************************************** */
  1977. /*                                    */
  1978. /*            Terminal and Termcap                */
  1979. /*                                    */
  1980. /* **************************************************************** */
  1981.  
  1982. static char *term_buffer = (char *)NULL;
  1983. static char *term_string_buffer = (char *)NULL;
  1984.  
  1985. /* Non-zero means this terminal can't really do anything. */
  1986. int dumb_term = 0;
  1987.  
  1988. char PC;
  1989. char *BC, *UP;
  1990.  
  1991. /* Some strings to control terminal actions.  These are output by tputs (). */
  1992. char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
  1993.  
  1994. int screenwidth, screenheight;
  1995.  
  1996. /* Non-zero if we determine that the terminal can do character insertion. */
  1997. int terminal_can_insert = 0;
  1998.  
  1999. /* How to insert characters. */
  2000. char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
  2001.  
  2002. /* How to delete characters. */
  2003. char *term_dc, *term_DC;
  2004.  
  2005. #if defined (HACK_TERMCAP_MOTION)
  2006. char *term_forward_char;
  2007. #endif  /* HACK_TERMCAP_MOTION */
  2008.  
  2009. /* How to go up a line. */
  2010. char *term_up;
  2011.  
  2012. /* A visible bell, if the terminal can be made to flash the screen. */
  2013. char *visible_bell;
  2014.  
  2015. /* Re-initialize the terminal considering that the TERM/TERMCAP variable
  2016.    has changed. */
  2017. rl_reset_terminal (terminal_name)
  2018.      char *terminal_name;
  2019. {
  2020.   init_terminal_io (terminal_name);
  2021. }
  2022.  
  2023. init_terminal_io (terminal_name)
  2024.      char *terminal_name;
  2025. {
  2026. #ifdef __GO32__
  2027.   screenwidth = ScreenCols();
  2028.   screenheight = ScreenRows();
  2029.   term_cr = "\r";
  2030.   term_im = term_ei = term_ic = term_IC = (char *)NULL;
  2031.   term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  2032. #if defined (HACK_TERMCAP_MOTION)
  2033.       term_forward_char = (char *)NULL;
  2034. #endif
  2035.   terminal_can_insert = 0;
  2036.   return;
  2037. #else
  2038.   extern char *tgetstr ();
  2039.   char *term, *buffer;
  2040. #if defined (TIOCGWINSZ)
  2041.   struct winsize window_size;
  2042. #endif
  2043.   int tty;
  2044.  
  2045.   term = terminal_name ? terminal_name : getenv ("TERM");
  2046.  
  2047.   if (!term_string_buffer)
  2048.     term_string_buffer = (char *)xmalloc (2048);
  2049.  
  2050.   if (!term_buffer)
  2051.     term_buffer = (char *)xmalloc (2048);
  2052.  
  2053.   buffer = term_string_buffer;
  2054.  
  2055.   term_clrpag = term_cr = term_clreol = (char *)NULL;
  2056.  
  2057.   if (!term)
  2058.     term = "dumb";
  2059.  
  2060.   if (tgetent (term_buffer, term) < 0)
  2061.     {
  2062.       dumb_term = 1;
  2063.       screenwidth = 79;
  2064.       screenheight = 24;
  2065.       term_cr = "\r";
  2066.       term_im = term_ei = term_ic = term_IC = (char *)NULL;
  2067.       term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  2068. #if defined (HACK_TERMCAP_MOTION)
  2069.       term_forward_char = (char *)NULL;
  2070. #endif
  2071.       terminal_can_insert = 0;
  2072.       return;
  2073.     }
  2074.  
  2075.   BC = tgetstr ("pc", &buffer);
  2076.   PC = buffer ? *buffer : 0;
  2077.  
  2078.   term_backspace = tgetstr ("le", &buffer);
  2079.  
  2080.   term_cr = tgetstr ("cr", &buffer);
  2081.   term_clreol = tgetstr ("ce", &buffer);
  2082.   term_clrpag = tgetstr ("cl", &buffer);
  2083.  
  2084.   if (!term_cr)
  2085.     term_cr =  "\r";
  2086.  
  2087. #if defined (HACK_TERMCAP_MOTION)
  2088.   term_forward_char = tgetstr ("nd", &buffer);
  2089. #endif  /* HACK_TERMCAP_MOTION */
  2090.  
  2091.   if (rl_instream)
  2092.     tty = fileno (rl_instream);
  2093.   else
  2094.     tty = 0;
  2095.  
  2096.   screenwidth = screenheight = 0;
  2097. #if defined (TIOCGWINSZ)
  2098.   if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
  2099.     {
  2100.       screenwidth = (int) window_size.ws_col;
  2101.       screenheight = (int) window_size.ws_row;
  2102.     }
  2103. #endif
  2104.  
  2105.   if (screenwidth <= 0 || screenheight <= 0)
  2106.     {
  2107.       screenwidth = tgetnum ("co");
  2108.       screenheight = tgetnum ("li");
  2109.     }
  2110.  
  2111.   screenwidth--;
  2112.  
  2113.   if (screenwidth <= 0)
  2114.     screenwidth = 79;
  2115.  
  2116.   if (screenheight <= 0)
  2117.     screenheight = 24;
  2118.  
  2119.   term_im = tgetstr ("im", &buffer);
  2120.   term_ei = tgetstr ("ei", &buffer);
  2121.   term_IC = tgetstr ("IC", &buffer);
  2122.   term_ic = tgetstr ("ic", &buffer);
  2123.  
  2124.   /* "An application program can assume that the terminal can do
  2125.       character insertion if *any one of* the capabilities `IC',
  2126.       `im', `ic' or `ip' is provided."  But we can't do anything if
  2127.       only `ip' is provided, so... */
  2128.   terminal_can_insert = (term_IC || term_im || term_ic);
  2129.  
  2130.   term_up = tgetstr ("up", &buffer);
  2131.   term_dc = tgetstr ("dc", &buffer);
  2132.   term_DC = tgetstr ("DC", &buffer);
  2133.  
  2134.   visible_bell = tgetstr ("vb", &buffer);
  2135. #endif /* !__GO32__ */
  2136. }
  2137.  
  2138. /* A function for the use of tputs () */
  2139. static void
  2140. output_character_function (c)
  2141.      int c;
  2142. {
  2143.   putc (c, out_stream);
  2144. }
  2145.  
  2146. /* Write COUNT characters from STRING to the output stream. */
  2147. static void
  2148. output_some_chars (string, count)
  2149.      char *string;
  2150.      int count;
  2151. {
  2152.   fwrite (string, 1, count, out_stream);
  2153. }
  2154.  
  2155. /* Delete COUNT characters from the display line. */
  2156. static
  2157. delete_chars (count)
  2158.      int count;
  2159. {
  2160. #ifdef __GO32__
  2161.   int r, c, w;
  2162.   ScreenGetCursor(&r, &c);
  2163.   w = ScreenCols();
  2164.   memcpy(ScreenPrimary+r*w+c, ScreenPrimary+r*w+c+count, w-c-count);
  2165.   memset(ScreenPrimary+r*w+w-count, 0, count*2);
  2166. #else /* __GO32__ */
  2167.   if (count > screenwidth)
  2168.     return;
  2169.  
  2170.   if (term_DC && *term_DC)
  2171.     {
  2172.       char *tgoto (), *buffer;
  2173.       buffer = tgoto (term_DC, 0, count);
  2174.       tputs (buffer, 1, output_character_function);
  2175.     }
  2176.   else
  2177.     {
  2178.       if (term_dc && *term_dc)
  2179.     while (count--)
  2180.       tputs (term_dc, 1, output_character_function);
  2181.     }
  2182. #endif /* __GO32__ */
  2183. }
  2184.  
  2185. /* Insert COUNT characters from STRING to the output stream. */
  2186. static
  2187. insert_some_chars (string, count)
  2188.      char *string;
  2189.      int count;
  2190. {
  2191. #ifdef __GO32__
  2192.   int r, c, w;
  2193.   ScreenGetCursor(&r, &c);
  2194.   w = ScreenCols();
  2195.   memcpy(ScreenPrimary+r*w+c+count, ScreenPrimary+r*w+c, w-c-count);
  2196.   /* Print the text. */
  2197.   output_some_chars (string, count);
  2198. #else /* __GO32__ */
  2199.   /* If IC is defined, then we do not have to "enter" insert mode. */
  2200.   if (term_IC)
  2201.     {
  2202.       char *tgoto (), *buffer;
  2203.       buffer = tgoto (term_IC, 0, count);
  2204.       tputs (buffer, 1, output_character_function);
  2205.       output_some_chars (string, count);
  2206.     }
  2207.   else
  2208.     {
  2209.       register int i;
  2210.  
  2211.       /* If we have to turn on insert-mode, then do so. */
  2212.       if (term_im && *term_im)
  2213.     tputs (term_im, 1, output_character_function);
  2214.  
  2215.       /* If there is a special command for inserting characters, then
  2216.      use that first to open up the space. */
  2217.       if (term_ic && *term_ic)
  2218.     {
  2219.       for (i = count; i--; )
  2220.         tputs (term_ic, 1, output_character_function);
  2221.     }
  2222.  
  2223.       /* Print the text. */
  2224.       output_some_chars (string, count);
  2225.  
  2226.       /* If there is a string to turn off insert mode, we had best use
  2227.      it now. */
  2228.       if (term_ei && *term_ei)
  2229.     tputs (term_ei, 1, output_character_function);
  2230.     }
  2231. #endif /* __GO32__ */
  2232. }
  2233.  
  2234. /* Move the cursor back. */
  2235. backspace (count)
  2236.      int count;
  2237. {
  2238.   register int i;
  2239.  
  2240. #ifndef __GO32__
  2241.   if (term_backspace)
  2242.     for (i = 0; i < count; i++)
  2243.       tputs (term_backspace, 1, output_character_function);
  2244.   else
  2245. #endif /* !__GO32__ */
  2246.     for (i = 0; i < count; i++)
  2247.       putc ('\b', out_stream);
  2248. }
  2249.  
  2250. /* Move to the start of the next line. */
  2251. crlf ()
  2252. {
  2253. #if defined (NEW_TTY_DRIVER)
  2254.   tputs (term_cr, 1, output_character_function);
  2255. #endif /* NEW_TTY_DRIVER */
  2256.   putc ('\n', out_stream);
  2257. }
  2258.  
  2259. /* Clear to the end of the line.  COUNT is the minimum
  2260.    number of character spaces to clear, */
  2261. clear_to_eol (count)
  2262.      int count;
  2263. {
  2264. #ifndef __GO32__
  2265.   if (term_clreol)
  2266.     {
  2267.       tputs (term_clreol, 1, output_character_function);
  2268.     }
  2269.   else
  2270. #endif /* !__GO32__ */
  2271.     {
  2272.       register int i;
  2273.  
  2274.       /* Do one more character space. */
  2275.       count++;
  2276.  
  2277.       for (i = 0; i < count; i++)
  2278.     putc (' ', out_stream);
  2279.  
  2280.       backspace (count);
  2281.     }
  2282. }
  2283.  
  2284.  
  2285. /* **************************************************************** */
  2286. /*                                    */
  2287. /*              Saving and Restoring the TTY                */
  2288. /*                                    */
  2289. /* **************************************************************** */
  2290.  
  2291. /* Non-zero means that the terminal is in a prepped state. */
  2292. static int terminal_prepped = 0;
  2293.  
  2294. #if defined (NEW_TTY_DRIVER)
  2295.  
  2296. /* Standard flags, including ECHO. */
  2297. static int original_tty_flags = 0;
  2298.  
  2299. /* Local mode flags, like LPASS8. */
  2300. static int local_mode_flags = 0;
  2301.  
  2302. /* Terminal characters.  This has C-s and C-q in it. */
  2303. static struct tchars original_tchars;
  2304.  
  2305. /* Local special characters.  This has the interrupt characters in it. */
  2306. #if defined (TIOCGLTC)
  2307. static struct ltchars original_ltchars;
  2308. #endif
  2309.  
  2310. /* We use this to get and set the tty_flags. */
  2311. static struct sgttyb the_ttybuff;
  2312.  
  2313. /* Put the terminal in CBREAK mode so that we can detect key presses. */
  2314. static void
  2315. rl_prep_terminal ()
  2316. {
  2317. #ifndef __GO32__
  2318.   int tty = fileno (rl_instream);
  2319. #if defined (HAVE_BSD_SIGNALS)
  2320.   int oldmask;
  2321. #endif /* HAVE_BSD_SIGNALS */
  2322.  
  2323.   if (terminal_prepped)
  2324.     return;
  2325.  
  2326.   oldmask = sigblock (sigmask (SIGINT));
  2327.  
  2328.   /* We always get the latest tty values.  Maybe stty changed them. */
  2329.   ioctl (tty, TIOCGETP, &the_ttybuff);
  2330.   original_tty_flags = the_ttybuff.sg_flags;
  2331.  
  2332.   readline_echoing_p = (original_tty_flags & ECHO);
  2333.  
  2334. #if defined (TIOCLGET)
  2335.   ioctl (tty, TIOCLGET, &local_mode_flags);
  2336. #endif
  2337.  
  2338. #if !defined (ANYP)
  2339. #  define ANYP (EVENP | ODDP)
  2340. #endif
  2341.  
  2342.   /* If this terminal doesn't care how the 8th bit is used,
  2343.      then we can use it for the meta-key.  We check by seeing
  2344.      if BOTH odd and even parity are allowed. */
  2345.   if (the_ttybuff.sg_flags & ANYP)
  2346.     {
  2347. #if defined (PASS8)
  2348.       the_ttybuff.sg_flags |= PASS8;
  2349. #endif
  2350.  
  2351.       /* Hack on local mode flags if we can. */
  2352. #if defined (TIOCLGET) && defined (LPASS8)
  2353.       {
  2354.     int flags;
  2355.     flags = local_mode_flags | LPASS8;
  2356.     ioctl (tty, TIOCLSET, &flags);
  2357.       }
  2358. #endif /* TIOCLGET && LPASS8 */
  2359.     }
  2360.  
  2361. #if defined (TIOCGETC)
  2362.   {
  2363.     struct tchars temp;
  2364.  
  2365.     ioctl (tty, TIOCGETC, &original_tchars);
  2366.     temp = original_tchars;
  2367.  
  2368. #if defined (USE_XON_XOFF)
  2369.     /* Get rid of C-s and C-q.
  2370.        We remember the value of startc (C-q) so that if the terminal is in
  2371.        xoff state, the user can xon it by pressing that character. */
  2372.     xon_char = temp.t_startc;
  2373.     temp.t_stopc = -1;
  2374.     temp.t_startc = -1;
  2375.  
  2376.     /* If there is an XON character, bind it to restart the output. */
  2377.     if (xon_char != -1)
  2378.       rl_bind_key (xon_char, rl_restart_output);
  2379. #endif /* USE_XON_XOFF */
  2380.  
  2381.     /* If there is an EOF char, bind eof_char to it. */
  2382.     if (temp.t_eofc != -1)
  2383.       eof_char = temp.t_eofc;
  2384.  
  2385. #if defined (NO_KILL_INTR)
  2386.     /* Get rid of C-\ and C-c. */
  2387.     temp.t_intrc = temp.t_quitc = -1;
  2388. #endif /* NO_KILL_INTR */
  2389.  
  2390.     ioctl (tty, TIOCSETC, &temp);
  2391.   }
  2392. #endif /* TIOCGETC */
  2393.  
  2394. #if defined (TIOCGLTC)
  2395.   {
  2396.     struct ltchars temp;
  2397.  
  2398.     ioctl (tty, TIOCGLTC, &original_ltchars);
  2399.     temp = original_ltchars;
  2400.  
  2401.     /* Make the interrupt keys go away.  Just enough to make people
  2402.        happy. */
  2403.     temp.t_dsuspc = -1;    /* C-y */
  2404.     temp.t_lnextc = -1;    /* C-v */
  2405.  
  2406.     ioctl (tty, TIOCSLTC, &temp);
  2407.   }
  2408. #endif /* TIOCGLTC */
  2409.  
  2410.   the_ttybuff.sg_flags &= ~(ECHO | CRMOD);
  2411.   the_ttybuff.sg_flags |= CBREAK;
  2412.   ioctl (tty, TIOCSETN, &the_ttybuff);
  2413.  
  2414.   terminal_prepped = 1;
  2415.  
  2416. #if defined (HAVE_BSD_SIGNALS)
  2417.   sigsetmask (oldmask);
  2418. #endif
  2419. #endif /* !__GO32__ */
  2420. }
  2421.  
  2422. /* Restore the terminal to its original state. */
  2423. static void
  2424. rl_deprep_terminal ()
  2425. {
  2426. #ifndef __GO32__
  2427.   int tty = fileno (rl_instream);
  2428. #if defined (HAVE_BSD_SIGNALS)
  2429.   int oldmask;
  2430. #endif
  2431.  
  2432.   if (!terminal_prepped)
  2433.     return;
  2434.  
  2435.   oldmask = sigblock (sigmask (SIGINT));
  2436.  
  2437.   the_ttybuff.sg_flags = original_tty_flags;
  2438.   ioctl (tty, TIOCSETN, &the_ttybuff);
  2439.   readline_echoing_p = 1;
  2440.  
  2441. #if defined (TIOCLGET)
  2442.   ioctl (tty, TIOCLSET, &local_mode_flags);
  2443. #endif
  2444.  
  2445. #if defined (TIOCSLTC)
  2446.   ioctl (tty, TIOCSLTC, &original_ltchars);
  2447. #endif
  2448.  
  2449. #if defined (TIOCSETC)
  2450.   ioctl (tty, TIOCSETC, &original_tchars);
  2451. #endif
  2452.   terminal_prepped = 0;
  2453.  
  2454. #if defined (HAVE_BSD_SIGNALS)
  2455.   sigsetmask (oldmask);
  2456. #endif
  2457. #endif /* !__GO32 */
  2458. }
  2459.  
  2460. #else  /* !defined (NEW_TTY_DRIVER) */
  2461.  
  2462. #if !defined (VMIN)
  2463. #define VMIN VEOF
  2464. #endif
  2465.  
  2466. #if !defined (VTIME)
  2467. #define VTIME VEOL
  2468. #endif
  2469.  
  2470. #ifndef __GO32__
  2471. #if defined (TERMIOS_TTY_DRIVER)
  2472. static struct termios otio;
  2473. #else
  2474. static struct termio otio;
  2475. #endif /* !TERMIOS_TTY_DRIVER */
  2476. #endif /* __GO32__ */
  2477.  
  2478. static void
  2479. rl_prep_terminal ()
  2480. {
  2481. #ifndef __GO32__
  2482.   int tty = fileno (rl_instream);
  2483. #if defined (TERMIOS_TTY_DRIVER)
  2484.   struct termios tio;
  2485. #else
  2486.   struct termio tio;
  2487. #endif /* !TERMIOS_TTY_DRIVER */
  2488.  
  2489. #if defined (HAVE_POSIX_SIGNALS)
  2490.   sigset_t set, oset;
  2491. #else
  2492. #  if defined (HAVE_BSD_SIGNALS)
  2493.   int oldmask;
  2494. #  endif /* HAVE_BSD_SIGNALS */
  2495. #endif /* !HAVE_POSIX_SIGNALS */
  2496.  
  2497.   if (terminal_prepped)
  2498.     return;
  2499.  
  2500.   /* Try to keep this function from being INTerrupted.  We can do it
  2501.      on POSIX and systems with BSD-like signal handling. */
  2502. #if defined (HAVE_POSIX_SIGNALS)
  2503.   sigemptyset (&set);
  2504.   sigaddset (&set, SIGINT);
  2505.   sigprocmask (SIG_BLOCK, &set, &oset);
  2506. #else /* !HAVE_POSIX_SIGNALS */
  2507. #  if defined (HAVE_BSD_SIGNALS)
  2508.   oldmask = sigblock (sigmask (SIGINT));
  2509. #  endif /* HAVE_BSD_SIGNALS */
  2510. #endif /* !HAVE_POSIX_SIGNALS */
  2511.  
  2512. #if defined (TERMIOS_TTY_DRIVER)
  2513.   tcgetattr (tty, &tio);
  2514. #else
  2515.   ioctl (tty, TCGETA, &tio);
  2516. #endif /* !TERMIOS_TTY_DRIVER */
  2517.  
  2518.   otio = tio;
  2519.  
  2520.   readline_echoing_p = (tio.c_lflag & ECHO);
  2521.  
  2522.   tio.c_lflag &= ~(ICANON|ECHO);
  2523.  
  2524.   if (otio.c_cc[VEOF] != _POSIX_VDISABLE)
  2525.     eof_char = otio.c_cc[VEOF];
  2526.  
  2527. #if defined (USE_XON_XOFF)
  2528. #if defined (IXANY)
  2529.   tio.c_iflag &= ~(IXON|IXOFF|IXANY);
  2530. #else
  2531.   /* `strict' Posix systems do not define IXANY. */
  2532.   tio.c_iflag &= ~(IXON|IXOFF);
  2533. #endif /* IXANY */
  2534. #endif /* USE_XON_XOFF */
  2535.  
  2536.   /* Only turn this off if we are using all 8 bits. */
  2537.   /* |ISTRIP|INPCK */
  2538.   tio.c_iflag &= ~(ISTRIP | INPCK);
  2539.  
  2540.   /* Make sure we differentiate between CR and NL on input. */
  2541.   tio.c_iflag &= ~(ICRNL | INLCR);
  2542.  
  2543. #if !defined (HANDLE_SIGNALS)
  2544.   tio.c_lflag &= ~ISIG;
  2545. #else
  2546.   tio.c_lflag |= ISIG;
  2547. #endif
  2548.  
  2549.   tio.c_cc[VMIN] = 1;
  2550.   tio.c_cc[VTIME] = 0;
  2551.  
  2552.   /* Turn off characters that we need on Posix systems with job control,
  2553.      just to be sure.  This includes ^Y and ^V.  This should not really
  2554.      be necessary.  */
  2555. #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_JOB_CONTROL)
  2556.  
  2557. #if defined (VLNEXT)
  2558.   tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
  2559. #endif
  2560.  
  2561. #if defined (VDSUSP)
  2562.   tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
  2563. #endif
  2564.  
  2565. #endif /* POSIX && JOB_CONTROL */
  2566.  
  2567. #if defined (TERMIOS_TTY_DRIVER)
  2568.   tcsetattr (tty, TCSADRAIN, &tio);
  2569.   tcflow (tty, TCOON);        /* Simulate a ^Q. */
  2570. #else
  2571.   ioctl (tty, TCSETAW, &tio);
  2572.   ioctl (tty, TCXONC, 1);    /* Simulate a ^Q. */
  2573. #endif /* !TERMIOS_TTY_DRIVER */
  2574.  
  2575.   terminal_prepped = 1;
  2576.  
  2577. #if defined (HAVE_POSIX_SIGNALS)
  2578.   sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
  2579. #else
  2580. #  if defined (HAVE_BSD_SIGNALS)
  2581.   sigsetmask (oldmask);
  2582. #  endif /* HAVE_BSD_SIGNALS */
  2583. #endif /* !HAVE_POSIX_SIGNALS */
  2584. #endif /* !__GO32__ */
  2585. }
  2586.  
  2587. static void
  2588. rl_deprep_terminal ()
  2589. {
  2590. #ifndef __GO32__ 
  2591.   int tty = fileno (rl_instream);
  2592.  
  2593.   /* Try to keep this function from being INTerrupted.  We can do it
  2594.      on POSIX and systems with BSD-like signal handling. */
  2595. #if defined (HAVE_POSIX_SIGNALS)
  2596.   sigset_t set, oset;
  2597. #else /* !HAVE_POSIX_SIGNALS */
  2598. #  if defined (HAVE_BSD_SIGNALS)
  2599.   int oldmask;
  2600. #  endif /* HAVE_BSD_SIGNALS */
  2601. #endif /* !HAVE_POSIX_SIGNALS */
  2602.  
  2603.   if (!terminal_prepped)
  2604.     return;
  2605.  
  2606. #if defined (HAVE_POSIX_SIGNALS)
  2607.   sigemptyset (&set);
  2608.   sigaddset (&set, SIGINT);
  2609.   sigprocmask (SIG_BLOCK, &set, &oset);
  2610. #else /* !HAVE_POSIX_SIGNALS */
  2611. #  if defined (HAVE_BSD_SIGNALS)
  2612.   oldmask = sigblock (sigmask (SIGINT));
  2613. #  endif /* HAVE_BSD_SIGNALS */
  2614. #endif /* !HAVE_POSIX_SIGNALS */
  2615.  
  2616. #if defined (TERMIOS_TTY_DRIVER)
  2617.   tcsetattr (tty, TCSADRAIN, &otio);
  2618.   tcflow (tty, TCOON);        /* Simulate a ^Q. */
  2619. #else /* TERMIOS_TTY_DRIVER */
  2620.   ioctl (tty, TCSETAW, &otio);
  2621.   ioctl (tty, TCXONC, 1);    /* Simulate a ^Q. */
  2622. #endif /* !TERMIOS_TTY_DRIVER */
  2623.  
  2624.   terminal_prepped = 0;
  2625.  
  2626. #if defined (HAVE_POSIX_SIGNALS)
  2627.   sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
  2628. #else /* !HAVE_POSIX_SIGNALS */
  2629. #  if defined (HAVE_BSD_SIGNALS)
  2630.   sigsetmask (oldmask);
  2631. #  endif /* HAVE_BSD_SIGNALS */
  2632. #endif /* !HAVE_POSIX_SIGNALS */
  2633. #endif /* !__GO32__ */
  2634. }
  2635. #endif  /* NEW_TTY_DRIVER */
  2636.  
  2637.  
  2638. /* **************************************************************** */
  2639. /*                                    */
  2640. /*            Utility Functions                */
  2641. /*                                    */
  2642. /* **************************************************************** */
  2643.  
  2644. /* Return 0 if C is not a member of the class of characters that belong
  2645.    in words, or 1 if it is. */
  2646.  
  2647. int allow_pathname_alphabetic_chars = 0;
  2648. char *pathname_alphabetic_chars = "/-_=~.#$";
  2649.  
  2650. int
  2651. alphabetic (c)
  2652.      int c;
  2653. {
  2654.   if (pure_alphabetic (c) || (numeric (c)))
  2655.     return (1);
  2656.  
  2657.   if (allow_pathname_alphabetic_chars)
  2658.     return ((int)rindex (pathname_alphabetic_chars, c));
  2659.   else
  2660.     return (0);
  2661. }
  2662.  
  2663. /* Return non-zero if C is a numeric character. */
  2664. int
  2665. numeric (c)
  2666.      int c;
  2667. {
  2668.   return (c >= '0' && c <= '9');
  2669. }
  2670.  
  2671. /* Ring the terminal bell. */
  2672. int
  2673. ding ()
  2674. {
  2675.   if (readline_echoing_p)
  2676.     {
  2677. #ifndef __GO32__
  2678.       if (prefer_visible_bell && visible_bell)
  2679.     tputs (visible_bell, 1, output_character_function);
  2680.       else
  2681. #endif /* !__GO32__ */
  2682.     {
  2683.       fprintf (stderr, "\007");
  2684.       fflush (stderr);
  2685.     }
  2686.     }
  2687.   return (-1);
  2688. }
  2689.  
  2690. /* How to abort things. */
  2691. rl_abort ()
  2692. {
  2693.   ding ();
  2694.   rl_clear_message ();
  2695.   rl_init_argument ();
  2696.   rl_pending_input = 0;
  2697.  
  2698.   defining_kbd_macro = 0;
  2699.   while (executing_macro)
  2700.     pop_executing_macro ();
  2701.  
  2702.   rl_last_func = (Function *)NULL;
  2703.   longjmp (readline_top_level, 1);
  2704. }
  2705.  
  2706. /* Return a copy of the string between FROM and TO.
  2707.    FROM is inclusive, TO is not. */
  2708. #if defined (sun) /* Yes, that's right, some crufty function in sunview is
  2709.              called rl_copy (). */
  2710. static
  2711. #endif
  2712. char *
  2713. rl_copy (from, to)
  2714.      int from, to;
  2715. {
  2716.   register int length;
  2717.   char *copy;
  2718.  
  2719.   /* Fix it if the caller is confused. */
  2720.   if (from > to)
  2721.     {
  2722.       int t = from;
  2723.       from = to;
  2724.       to = t;
  2725.     }
  2726.  
  2727.   length = to - from;
  2728.   copy = (char *)xmalloc (1 + length);
  2729.   strncpy (copy, the_line + from, length);
  2730.   copy[length] = '\0';
  2731.   return (copy);
  2732. }
  2733.  
  2734. /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
  2735.    LEN characters. */
  2736. void
  2737. rl_extend_line_buffer (len)
  2738.      int len;
  2739. {
  2740.   while (len >= rl_line_buffer_len)
  2741.     rl_line_buffer =
  2742.       (char *)xrealloc
  2743.     (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
  2744.  
  2745.   the_line = rl_line_buffer;
  2746. }
  2747.  
  2748.  
  2749. /* **************************************************************** */
  2750. /*                                    */
  2751. /*            Insert and Delete                */
  2752. /*                                    */
  2753. /* **************************************************************** */
  2754.  
  2755. /* Insert a string of text into the line at point.  This is the only
  2756.    way that you should do insertion.  rl_insert () calls this
  2757.    function. */
  2758. rl_insert_text (string)
  2759.      char *string;
  2760. {
  2761.   extern int doing_an_undo;
  2762.   register int i, l = strlen (string);
  2763.  
  2764.   if (rl_end + l >= rl_line_buffer_len)
  2765.     rl_extend_line_buffer (rl_end + l);
  2766.  
  2767.   for (i = rl_end; i >= rl_point; i--)
  2768.     the_line[i + l] = the_line[i];
  2769.   strncpy (the_line + rl_point, string, l);
  2770.  
  2771.   /* Remember how to undo this if we aren't undoing something. */
  2772.   if (!doing_an_undo)
  2773.     {
  2774.       /* If possible and desirable, concatenate the undos. */
  2775.       if ((strlen (string) == 1) &&
  2776.       rl_undo_list &&
  2777.       (rl_undo_list->what == UNDO_INSERT) &&
  2778.       (rl_undo_list->end == rl_point) &&
  2779.       (rl_undo_list->end - rl_undo_list->start < 20))
  2780.     rl_undo_list->end++;
  2781.       else
  2782.     rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
  2783.     }
  2784.   rl_point += l;
  2785.   rl_end += l;
  2786.   the_line[rl_end] = '\0';
  2787. }
  2788.  
  2789. /* Delete the string between FROM and TO.  FROM is
  2790.    inclusive, TO is not. */
  2791. rl_delete_text (from, to)
  2792.      int from, to;
  2793. {
  2794.   extern int doing_an_undo;
  2795.   register char *text;
  2796.  
  2797.   /* Fix it if the caller is confused. */
  2798.   if (from > to)
  2799.     {
  2800.       int t = from;
  2801.       from = to;
  2802.       to = t;
  2803.     }
  2804.   text = rl_copy (from, to);
  2805.   strncpy (the_line + from, the_line + to, rl_end - to);
  2806.  
  2807.   /* Remember how to undo this delete. */
  2808.   if (!doing_an_undo)
  2809.     rl_add_undo (UNDO_DELETE, from, to, text);
  2810.   else
  2811.     free (text);
  2812.  
  2813.   rl_end -= (to - from);
  2814.   the_line[rl_end] = '\0';
  2815. }
  2816.  
  2817.  
  2818. /* **************************************************************** */
  2819. /*                                    */
  2820. /*            Readline character functions            */
  2821. /*                                    */
  2822. /* **************************************************************** */
  2823.  
  2824. /* This is not a gap editor, just a stupid line input routine.  No hair
  2825.    is involved in writing any of the functions, and none should be. */
  2826.  
  2827. /* Note that:
  2828.  
  2829.    rl_end is the place in the string that we would place '\0';
  2830.    i.e., it is always safe to place '\0' there.
  2831.  
  2832.    rl_point is the place in the string where the cursor is.  Sometimes
  2833.    this is the same as rl_end.
  2834.  
  2835.    Any command that is called interactively receives two arguments.
  2836.    The first is a count: the numeric arg pased to this command.
  2837.    The second is the key which invoked this command.
  2838. */
  2839.  
  2840.  
  2841. /* **************************************************************** */
  2842. /*                                    */
  2843. /*            Movement Commands                */
  2844. /*                                    */
  2845. /* **************************************************************** */
  2846.  
  2847. /* Note that if you `optimize' the display for these functions, you cannot
  2848.    use said functions in other functions which do not do optimizing display.
  2849.    I.e., you will have to update the data base for rl_redisplay, and you
  2850.    might as well let rl_redisplay do that job. */
  2851.  
  2852. /* Move forward COUNT characters. */
  2853. rl_forward (count)
  2854.      int count;
  2855. {
  2856.   if (count < 0)
  2857.     rl_backward (-count);
  2858.   else
  2859.     while (count)
  2860.       {
  2861. #if defined (VI_MODE)
  2862.     if (rl_point == (rl_end - (rl_editing_mode == vi_mode)))
  2863. #else
  2864.     if (rl_point == rl_end)
  2865. #endif /* VI_MODE */
  2866.       {
  2867.         ding ();
  2868.         return;
  2869.       }
  2870.     else
  2871.       rl_point++;
  2872.     --count;
  2873.       }
  2874. }
  2875.  
  2876. /* Move backward COUNT characters. */
  2877. rl_backward (count)
  2878.      int count;
  2879. {
  2880.   if (count < 0)
  2881.     rl_forward (-count);
  2882.   else
  2883.     while (count)
  2884.       {
  2885.     if (!rl_point)
  2886.       {
  2887.         ding ();
  2888.         return;
  2889.       }
  2890.     else
  2891.       --rl_point;
  2892.     --count;
  2893.       }
  2894. }
  2895.  
  2896. /* Move to the beginning of the line. */
  2897. rl_beg_of_line ()
  2898. {
  2899.   rl_point = 0;
  2900. }
  2901.  
  2902. /* Move to the end of the line. */
  2903. rl_end_of_line ()
  2904. {
  2905.   rl_point = rl_end;
  2906. }
  2907.  
  2908. /* Move forward a word.  We do what Emacs does. */
  2909. rl_forward_word (count)
  2910.      int count;
  2911. {
  2912.   int c;
  2913.  
  2914.   if (count < 0)
  2915.     {
  2916.       rl_backward_word (-count);
  2917.       return;
  2918.     }
  2919.  
  2920.   while (count)
  2921.     {
  2922.       if (rl_point == rl_end)
  2923.     return;
  2924.  
  2925.       /* If we are not in a word, move forward until we are in one.
  2926.      Then, move forward until we hit a non-alphabetic character. */
  2927.       c = the_line[rl_point];
  2928.       if (!alphabetic (c))
  2929.     {
  2930.       while (++rl_point < rl_end)
  2931.         {
  2932.           c = the_line[rl_point];
  2933.           if (alphabetic (c)) break;
  2934.         }
  2935.     }
  2936.       if (rl_point == rl_end) return;
  2937.       while (++rl_point < rl_end)
  2938.     {
  2939.       c = the_line[rl_point];
  2940.       if (!alphabetic (c)) break;
  2941.     }
  2942.       --count;
  2943.     }
  2944. }
  2945.  
  2946. /* Move backward a word.  We do what Emacs does. */
  2947. rl_backward_word (count)
  2948.      int count;
  2949. {
  2950.   int c;
  2951.  
  2952.   if (count < 0)
  2953.     {
  2954.       rl_forward_word (-count);
  2955.       return;
  2956.     }
  2957.  
  2958.   while (count)
  2959.     {
  2960.       if (!rl_point)
  2961.     return;
  2962.  
  2963.       /* Like rl_forward_word (), except that we look at the characters
  2964.      just before point. */
  2965.  
  2966.       c = the_line[rl_point - 1];
  2967.       if (!alphabetic (c))
  2968.     {
  2969.       while (--rl_point)
  2970.         {
  2971.           c = the_line[rl_point - 1];
  2972.           if (alphabetic (c)) break;
  2973.         }
  2974.     }
  2975.  
  2976.       while (rl_point)
  2977.     {
  2978.       c = the_line[rl_point - 1];
  2979.       if (!alphabetic (c))
  2980.         break;
  2981.       else --rl_point;
  2982.     }
  2983.       --count;
  2984.     }
  2985. }
  2986.  
  2987. /* Clear the current line.  Numeric argument to C-l does this. */
  2988. rl_refresh_line ()
  2989. {
  2990.   int curr_line = last_c_pos / screenwidth;
  2991.   extern char *term_clreol;
  2992.  
  2993.   move_vert(curr_line);
  2994.   move_cursor_relative (0, the_line);   /* XXX is this right */
  2995.  
  2996. #ifdef __GO32__
  2997.   {
  2998.   int r, c, w;
  2999.   ScreenGetCursor(&r, &c);
  3000.   w = ScreenCols();
  3001.   memset(ScreenPrimary+r*w+c, 0, (w-c)*2);
  3002.   }
  3003. #else /* __GO32__ */
  3004.   if (term_clreol)
  3005.     tputs (term_clreol, 1, output_character_function);
  3006. #endif /* __GO32__/else */
  3007.  
  3008.   rl_forced_update_display ();
  3009.   rl_display_fixed = 1;
  3010. }
  3011.  
  3012. /* C-l typed to a line without quoting clears the screen, and then reprints
  3013.    the prompt and the current input line.  Given a numeric arg, redraw only
  3014.    the current line. */
  3015. rl_clear_screen ()
  3016. {
  3017.   extern char *term_clrpag;
  3018.  
  3019.   if (rl_explicit_arg)
  3020.     {
  3021.       rl_refresh_line ();
  3022.       return;
  3023.     }
  3024.  
  3025. #ifndef __GO32__
  3026.   if (term_clrpag)
  3027.     tputs (term_clrpag, 1, output_character_function);
  3028.   else
  3029. #endif /* !__GO32__ */
  3030.     crlf ();
  3031.  
  3032.   rl_forced_update_display ();
  3033.   rl_display_fixed = 1;
  3034. }
  3035.  
  3036. rl_arrow_keys (count, c)
  3037.      int count, c;
  3038. {
  3039.   int ch;
  3040.  
  3041.   ch = rl_read_key ();
  3042.  
  3043.   switch (to_upper (ch))
  3044.     {
  3045.     case 'A':
  3046.       rl_get_previous_history (count);
  3047.       break;
  3048.  
  3049.     case 'B':
  3050.       rl_get_next_history (count);
  3051.       break;
  3052.  
  3053.     case 'C':
  3054.       rl_forward (count);
  3055.       break;
  3056.  
  3057.     case 'D':
  3058.       rl_backward (count);
  3059.       break;
  3060.  
  3061.     default:
  3062.       ding ();
  3063.     }
  3064. }
  3065.  
  3066.  
  3067. /* **************************************************************** */
  3068. /*                                    */
  3069. /*            Text commands                    */
  3070. /*                                    */
  3071. /* **************************************************************** */
  3072.  
  3073. /* Insert the character C at the current location, moving point forward. */
  3074. rl_insert (count, c)
  3075.      int count, c;
  3076. {
  3077.   register int i;
  3078.   char *string;
  3079.  
  3080.   if (count <= 0)
  3081.     return;
  3082.  
  3083.   /* If we can optimize, then do it.  But don't let people crash
  3084.      readline because of extra large arguments. */
  3085.   if (count > 1 && count < 1024)
  3086.     {
  3087.       string = (char *)alloca (1 + count);
  3088.  
  3089.       for (i = 0; i < count; i++)
  3090.     string[i] = c;
  3091.  
  3092.       string[i] = '\0';
  3093.       rl_insert_text (string);
  3094.       return;
  3095.     }
  3096.  
  3097.   if (count > 1024)
  3098.     {
  3099.       int decreaser;
  3100.  
  3101.       string = (char *)alloca (1024 + 1);
  3102.  
  3103.       for (i = 0; i < 1024; i++)
  3104.     string[i] = c;
  3105.  
  3106.       while (count)
  3107.     {
  3108.       decreaser = (count > 1024 ? 1024 : count);
  3109.       string[decreaser] = '\0';
  3110.       rl_insert_text (string);
  3111.       count -= decreaser;
  3112.     }
  3113.       return;
  3114.     }
  3115.  
  3116.   /* We are inserting a single character.
  3117.      If there is pending input, then make a string of all of the
  3118.      pending characters that are bound to rl_insert, and insert
  3119.      them all. */
  3120.   if (any_typein)
  3121.     {
  3122.       int key = 0, t;
  3123.  
  3124.       i = 0;
  3125.       string = (char *)alloca (ibuffer_len + 1);
  3126.       string[i++] = c;
  3127.  
  3128.       while ((t = rl_get_char (&key)) &&
  3129.          (keymap[key].type == ISFUNC &&
  3130.           keymap[key].function == rl_insert))
  3131.     string[i++] = key;
  3132.  
  3133.       if (t)
  3134.     rl_unget_char (key);
  3135.  
  3136.       string[i] = '\0';
  3137.       rl_insert_text (string);
  3138.       return;
  3139.     }
  3140.   else
  3141.     {
  3142.       /* Inserting a single character. */
  3143.       string = (char *)alloca (2);
  3144.  
  3145.       string[1] = '\0';
  3146.       string[0] = c;
  3147.       rl_insert_text (string);
  3148.     }
  3149. }
  3150.  
  3151. /* Insert the next typed character verbatim. */
  3152. rl_quoted_insert (count)
  3153.      int count;
  3154. {
  3155.   int c = rl_read_key ();
  3156.   rl_insert (count, c);
  3157. }
  3158.  
  3159. /* Insert a tab character. */
  3160. rl_tab_insert (count)
  3161.      int count;
  3162. {
  3163.   rl_insert (count, '\t');
  3164. }
  3165.  
  3166. /* What to do when a NEWLINE is pressed.  We accept the whole line.
  3167.    KEY is the key that invoked this command.  I guess it could have
  3168.    meaning in the future. */
  3169. rl_newline (count, key)
  3170.      int count, key;
  3171. {
  3172.  
  3173.   rl_done = 1;
  3174.  
  3175. #if defined (VI_MODE)
  3176.   {
  3177.     extern int vi_doing_insert;
  3178.     if (vi_doing_insert)
  3179.       {
  3180.     rl_end_undo_group ();
  3181.     vi_doing_insert = 0;
  3182.       }
  3183.   }
  3184. #endif /* VI_MODE */
  3185.  
  3186.   if (readline_echoing_p)
  3187.     {
  3188.       move_vert (vis_botlin);
  3189.       vis_botlin = 0;
  3190.       crlf ();
  3191.       fflush (out_stream);
  3192.       rl_display_fixed++;
  3193.     }
  3194. }
  3195.  
  3196. rl_clean_up_for_exit ()
  3197. {
  3198.   if (readline_echoing_p)
  3199.     {
  3200.       move_vert (vis_botlin);
  3201.       vis_botlin = 0;
  3202.       fflush (out_stream);
  3203.       rl_restart_output ();
  3204.     }
  3205. }
  3206.  
  3207. /* What to do for some uppercase characters, like meta characters,
  3208.    and some characters appearing in emacs_ctlx_keymap.  This function
  3209.    is just a stub, you bind keys to it and the code in rl_dispatch ()
  3210.    is special cased. */
  3211. rl_do_lowercase_version (ignore1, ignore2)
  3212.      int ignore1, ignore2;
  3213. {
  3214. }
  3215.  
  3216. /* Rubout the character behind point. */
  3217. rl_rubout (count)
  3218.      int count;
  3219. {
  3220.   if (count < 0)
  3221.     {
  3222.       rl_delete (-count);
  3223.       return;
  3224.     }
  3225.  
  3226.   if (!rl_point)
  3227.     {
  3228.       ding ();
  3229.       return;
  3230.     }
  3231.  
  3232.   if (count > 1)
  3233.     {
  3234.       int orig_point = rl_point;
  3235.       rl_backward (count);
  3236.       rl_kill_text (orig_point, rl_point);
  3237.     }
  3238.   else
  3239.     {
  3240.       int c = the_line[--rl_point];
  3241.       rl_delete_text (rl_point, rl_point + 1);
  3242.  
  3243.       if (rl_point == rl_end && alphabetic (c) && last_c_pos)
  3244.     {
  3245.       backspace (1);
  3246.       putc (' ', out_stream);
  3247.       backspace (1);
  3248.       last_c_pos--;
  3249.       visible_line[last_c_pos] = '\0';
  3250.       rl_display_fixed++;
  3251.     }
  3252.     }
  3253. }
  3254.  
  3255. /* Delete the character under the cursor.  Given a numeric argument,
  3256.    kill that many characters instead. */
  3257. rl_delete (count, invoking_key)
  3258.      int count, invoking_key;
  3259. {
  3260.   if (count < 0)
  3261.     {
  3262.       rl_rubout (-count);
  3263.       return;
  3264.     }
  3265.  
  3266.   if (rl_point == rl_end)
  3267.     {
  3268.       ding ();
  3269.       return;
  3270.     }
  3271.  
  3272.   if (count > 1)
  3273.     {
  3274.       int orig_point = rl_point;
  3275.       rl_forward (count);
  3276.       rl_kill_text (orig_point, rl_point);
  3277.       rl_point = orig_point;
  3278.     }
  3279.   else
  3280.     rl_delete_text (rl_point, rl_point + 1);
  3281. }
  3282.  
  3283.  
  3284. /* **************************************************************** */
  3285. /*                                    */
  3286. /*            Kill commands                    */
  3287. /*                                    */
  3288. /* **************************************************************** */
  3289.  
  3290. /* The next two functions mimic unix line editing behaviour, except they
  3291.    save the deleted text on the kill ring.  This is safer than not saving
  3292.    it, and since we have a ring, nobody should get screwed. */
  3293.  
  3294. /* This does what C-w does in Unix.  We can't prevent people from
  3295.    using behaviour that they expect. */
  3296. rl_unix_word_rubout ()
  3297. {
  3298.   if (!rl_point) ding ();
  3299.   else {
  3300.     int orig_point = rl_point;
  3301.     while (rl_point && whitespace (the_line[rl_point - 1]))
  3302.       rl_point--;
  3303.     while (rl_point && !whitespace (the_line[rl_point - 1]))
  3304.       rl_point--;
  3305.     rl_kill_text (rl_point, orig_point);
  3306.   }
  3307. }
  3308.  
  3309. /* Here is C-u doing what Unix does.  You don't *have* to use these
  3310.    key-bindings.  We have a choice of killing the entire line, or
  3311.    killing from where we are to the start of the line.  We choose the
  3312.    latter, because if you are a Unix weenie, then you haven't backspaced
  3313.    into the line at all, and if you aren't, then you know what you are
  3314.    doing. */
  3315. rl_unix_line_discard ()
  3316. {
  3317.   if (!rl_point) ding ();
  3318.   else {
  3319.     rl_kill_text (rl_point, 0);
  3320.     rl_point = 0;
  3321.   }
  3322. }
  3323.  
  3324.  
  3325.  
  3326. /* **************************************************************** */
  3327. /*                                    */
  3328. /*            Commands For Typos                */
  3329. /*                                    */
  3330. /* **************************************************************** */
  3331.  
  3332. /* Random and interesting things in here.  */
  3333.  
  3334. /* **************************************************************** */
  3335. /*                                    */
  3336. /*            Changing Case                    */
  3337. /*                                    */
  3338. /* **************************************************************** */
  3339.  
  3340. /* The three kinds of things that we know how to do. */
  3341. #define UpCase 1
  3342. #define DownCase 2
  3343. #define CapCase 3
  3344.  
  3345. /* Uppercase the word at point. */
  3346. rl_upcase_word (count)
  3347.      int count;
  3348. {
  3349.   rl_change_case (count, UpCase);
  3350. }
  3351.  
  3352. /* Lowercase the word at point. */
  3353. rl_downcase_word (count)
  3354.      int count;
  3355. {
  3356.   rl_change_case (count, DownCase);
  3357. }
  3358.  
  3359. /* Upcase the first letter, downcase the rest. */
  3360. rl_capitalize_word (count)
  3361.      int count;
  3362. {
  3363.   rl_change_case (count, CapCase);
  3364. }
  3365.  
  3366. /* The meaty function.
  3367.    Change the case of COUNT words, performing OP on them.
  3368.    OP is one of UpCase, DownCase, or CapCase.
  3369.    If a negative argument is given, leave point where it started,
  3370.    otherwise, leave it where it moves to. */
  3371. rl_change_case (count, op)
  3372.      int count, op;
  3373. {
  3374.   register int start = rl_point, end;
  3375.   int state = 0;
  3376.  
  3377.   rl_forward_word (count);
  3378.   end = rl_point;
  3379.  
  3380.   if (count < 0)
  3381.     {
  3382.       int temp = start;
  3383.       start = end;
  3384.       end = temp;
  3385.     }
  3386.  
  3387.   /* We are going to modify some text, so let's prepare to undo it. */
  3388.   rl_modifying (start, end);
  3389.  
  3390.   for (; start < end; start++)
  3391.     {
  3392.       switch (op)
  3393.     {
  3394.     case UpCase:
  3395.       the_line[start] = to_upper (the_line[start]);
  3396.       break;
  3397.  
  3398.     case DownCase:
  3399.       the_line[start] = to_lower (the_line[start]);
  3400.       break;
  3401.  
  3402.     case CapCase:
  3403.       if (state == 0)
  3404.         {
  3405.           the_line[start] = to_upper (the_line[start]);
  3406.           state = 1;
  3407.         }
  3408.       else
  3409.         {
  3410.           the_line[start] = to_lower (the_line[start]);
  3411.         }
  3412.       if (!pure_alphabetic (the_line[start]))
  3413.         state = 0;
  3414.       break;
  3415.  
  3416.     default:
  3417.       abort ();
  3418.     }
  3419.     }
  3420.   rl_point = end;
  3421. }
  3422.  
  3423. /* **************************************************************** */
  3424. /*                                    */
  3425. /*            Transposition                    */
  3426. /*                                    */
  3427. /* **************************************************************** */
  3428.  
  3429. /* Transpose the words at point. */
  3430. rl_transpose_words (count)
  3431.      int count;
  3432. {
  3433.   char *word1, *word2;
  3434.   int w1_beg, w1_end, w2_beg, w2_end;
  3435.   int orig_point = rl_point;
  3436.  
  3437.   if (!count) return;
  3438.  
  3439.   /* Find the two words. */
  3440.   rl_forward_word (count);
  3441.   w2_end = rl_point;
  3442.   rl_backward_word (1);
  3443.   w2_beg = rl_point;
  3444.   rl_backward_word (count);
  3445.   w1_beg = rl_point;
  3446.   rl_forward_word (1);
  3447.   w1_end = rl_point;
  3448.  
  3449.   /* Do some check to make sure that there really are two words. */
  3450.   if ((w1_beg == w2_beg) || (w2_beg < w1_end))
  3451.     {
  3452.       ding ();
  3453.       rl_point = orig_point;
  3454.       return;
  3455.     }
  3456.  
  3457.   /* Get the text of the words. */
  3458.   word1 = rl_copy (w1_beg, w1_end);
  3459.   word2 = rl_copy (w2_beg, w2_end);
  3460.  
  3461.   /* We are about to do many insertions and deletions.  Remember them
  3462.      as one operation. */
  3463.   rl_begin_undo_group ();
  3464.  
  3465.   /* Do the stuff at word2 first, so that we don't have to worry
  3466.      about word1 moving. */
  3467.   rl_point = w2_beg;
  3468.   rl_delete_text (w2_beg, w2_end);
  3469.   rl_insert_text (word1);
  3470.  
  3471.   rl_point = w1_beg;
  3472.   rl_delete_text (w1_beg, w1_end);
  3473.   rl_insert_text (word2);
  3474.  
  3475.   /* This is exactly correct since the text before this point has not
  3476.      changed in length. */
  3477.   rl_point = w2_end;
  3478.  
  3479.   /* I think that does it. */
  3480.   rl_end_undo_group ();
  3481.   free (word1); free (word2);
  3482. }
  3483.  
  3484. /* Transpose the characters at point.  If point is at the end of the line,
  3485.    then transpose the characters before point. */
  3486. rl_transpose_chars (count)
  3487.      int count;
  3488. {
  3489.   if (!count)
  3490.     return;
  3491.  
  3492.   if (!rl_point || rl_end < 2) {
  3493.     ding ();
  3494.     return;
  3495.   }
  3496.  
  3497.   while (count)
  3498.     {
  3499.       if (rl_point == rl_end)
  3500.     {
  3501.       int t = the_line[rl_point - 1];
  3502.  
  3503.       the_line[rl_point - 1] = the_line[rl_point - 2];
  3504.       the_line[rl_point - 2] = t;
  3505.     }
  3506.       else
  3507.     {
  3508.       int t = the_line[rl_point];
  3509.  
  3510.       the_line[rl_point] = the_line[rl_point - 1];
  3511.       the_line[rl_point - 1] = t;
  3512.  
  3513.       if (count < 0 && rl_point)
  3514.         rl_point--;
  3515.       else
  3516.         rl_point++;
  3517.     }
  3518.  
  3519.       if (count < 0)
  3520.     count++;
  3521.       else
  3522.     count--;
  3523.     }
  3524. }
  3525.  
  3526.  
  3527. /* **************************************************************** */
  3528. /*                                    */
  3529. /*            Bogus Flow Control                  */
  3530. /*                                    */
  3531. /* **************************************************************** */
  3532.  
  3533. rl_restart_output (count, key)
  3534.      int count, key;
  3535. {
  3536.   int fildes = fileno (rl_outstream);
  3537. #if defined (TIOCSTART)
  3538. #if defined (apollo)
  3539.   ioctl (&fildes, TIOCSTART, 0);
  3540. #else
  3541.   ioctl (fildes, TIOCSTART, 0);
  3542. #endif /* apollo */
  3543.  
  3544. #else
  3545. #  if defined (TERMIOS_TTY_DRIVER)
  3546.         tcflow (fildes, TCOON);
  3547. #  else
  3548. #    if defined (TCXONC)
  3549.         ioctl (fildes, TCXONC, TCOON);
  3550. #    endif /* TCXONC */
  3551. #  endif /* !TERMIOS_TTY_DRIVER */
  3552. #endif /* TIOCSTART */
  3553. }
  3554.  
  3555. rl_stop_output (count, key)
  3556.      int count, key;
  3557. {
  3558.   int fildes = fileno (rl_instream);
  3559.  
  3560. #if defined (TIOCSTOP)
  3561. # if defined (apollo)
  3562.   ioctl (&fildes, TIOCSTOP, 0);
  3563. # else
  3564.   ioctl (fildes, TIOCSTOP, 0);
  3565. # endif /* apollo */
  3566. #else
  3567. # if defined (TERMIOS_TTY_DRIVER)
  3568.   tcflow (fildes, TCOOFF);
  3569. # else
  3570. #   if defined (TCXONC)
  3571.   ioctl (fildes, TCXONC, TCOON);
  3572. #   endif /* TCXONC */
  3573. # endif /* !TERMIOS_TTY_DRIVER */
  3574. #endif /* TIOCSTOP */
  3575. }
  3576.  
  3577. /* **************************************************************** */
  3578. /*                                    */
  3579. /*    Completion matching, from readline's point of view.        */
  3580. /*                                    */
  3581. /* **************************************************************** */
  3582.  
  3583. /* Pointer to the generator function for completion_matches ().
  3584.    NULL means to use filename_entry_function (), the default filename
  3585.    completer. */
  3586. Function *rl_completion_entry_function = (Function *)NULL;
  3587.  
  3588. /* Pointer to alternative function to create matches.
  3589.    Function is called with TEXT, START, and END.
  3590.    START and END are indices in RL_LINE_BUFFER saying what the boundaries
  3591.    of TEXT are.
  3592.    If this function exists and returns NULL then call the value of
  3593.    rl_completion_entry_function to try to match, otherwise use the
  3594.    array of strings returned. */
  3595. Function *rl_attempted_completion_function = (Function *)NULL;
  3596.  
  3597. /* Local variable states what happened during the last completion attempt. */
  3598. static int completion_changed_buffer = 0;
  3599.  
  3600. /* Complete the word at or before point.  You have supplied the function
  3601.    that does the initial simple matching selection algorithm (see
  3602.    completion_matches ()).  The default is to do filename completion. */
  3603.  
  3604. rl_complete (ignore, invoking_key)
  3605.      int ignore, invoking_key;
  3606. {
  3607.   if (rl_last_func == rl_complete && !completion_changed_buffer)
  3608.     rl_complete_internal ('?');
  3609.   else
  3610.     rl_complete_internal (TAB);
  3611. }
  3612.  
  3613. /* List the possible completions.  See description of rl_complete (). */
  3614. rl_possible_completions ()
  3615. {
  3616.   rl_complete_internal ('?');
  3617. }
  3618.  
  3619. /* The user must press "y" or "n". Non-zero return means "y" pressed. */
  3620. get_y_or_n ()
  3621. {
  3622.   int c;
  3623.  loop:
  3624.   c = rl_read_key ();
  3625.   if (c == 'y' || c == 'Y') return (1);
  3626.   if (c == 'n' || c == 'N') return (0);
  3627.   if (c == ABORT_CHAR) rl_abort ();
  3628.   ding (); goto loop;
  3629. }
  3630.  
  3631. /* Up to this many items will be displayed in response to a
  3632.    possible-completions call.  After that, we ask the user if
  3633.    she is sure she wants to see them all. */
  3634. int rl_completion_query_items = 100;
  3635.  
  3636. /* The basic list of characters that signal a break between words for the
  3637.    completer routine.  The contents of this variable is what breaks words
  3638.    in the shell, i.e. " \t\n\"\\'`@@$><=" */
  3639. char *rl_basic_word_break_characters = " \t\n\"\\'`@@$><=;|&{(";
  3640.  
  3641. /* The list of characters that signal a break between words for
  3642.    rl_complete_internal.  The default list is the contents of
  3643.    rl_basic_word_break_characters.  */
  3644. char *rl_completer_word_break_characters = (char *)NULL;
  3645.  
  3646. /* List of characters that are word break characters, but should be left
  3647.    in TEXT when it is passed to the completion function.  The shell uses
  3648.    this to help determine what kind of completing to do. */
  3649. char *rl_special_prefixes = (char *)NULL;
  3650.  
  3651. /* If non-zero, then disallow duplicates in the matches. */
  3652. int rl_ignore_completion_duplicates = 1;
  3653.  
  3654. /* Non-zero means that the results of the matches are to be treated
  3655.    as filenames.  This is ALWAYS zero on entry, and can only be changed
  3656.    within a completion entry finder function. */
  3657. int rl_filename_completion_desired = 0;
  3658.  
  3659. /* This function, if defined, is called by the completer when real
  3660.    filename completion is done, after all the matching names have been
  3661.    generated. It is passed a (char**) known as matches in the code below.
  3662.    It consists of a NULL-terminated array of pointers to potential
  3663.    matching strings.  The 1st element (matches[0]) is the maximal
  3664.    substring that is common to all matches. This function can re-arrange
  3665.    the list of matches as required, but all elements of the array must be
  3666.    free()'d if they are deleted. The main intent of this function is
  3667.    to implement FIGNORE a la SunOS csh. */
  3668. Function *rl_ignore_some_completions_function = (Function *)NULL;
  3669.  
  3670. /* Complete the word at or before point.
  3671.    WHAT_TO_DO says what to do with the completion.
  3672.    `?' means list the possible completions.
  3673.    TAB means do standard completion.
  3674.    `*' means insert all of the possible completions. */
  3675. rl_complete_internal (what_to_do)
  3676.      int what_to_do;
  3677. {
  3678.   char *filename_completion_function ();
  3679.   char **completion_matches (), **matches;
  3680.   Function *our_func;
  3681.   int start, end, delimiter = 0;
  3682.   char *text, *saved_line_buffer;
  3683.  
  3684.   if (the_line)
  3685.     saved_line_buffer = savestring (the_line);
  3686.   else
  3687.     saved_line_buffer = (char *)NULL;
  3688.  
  3689.   if (rl_completion_entry_function)
  3690.     our_func = rl_completion_entry_function;
  3691.   else
  3692.     our_func = (int (*)())filename_completion_function;
  3693.  
  3694.   /* Only the completion entry function can change this. */
  3695.   rl_filename_completion_desired = 0;
  3696.  
  3697.   /* We now look backwards for the start of a filename/variable word. */
  3698.   end = rl_point;
  3699.  
  3700.   if (rl_point)
  3701.     {
  3702.       while (--rl_point &&
  3703.          !rindex (rl_completer_word_break_characters, the_line[rl_point]));
  3704.  
  3705.       /* If we are at a word break, then advance past it. */
  3706.       if (rindex (rl_completer_word_break_characters, the_line[rl_point]))
  3707.     {
  3708.       /* If the character that caused the word break was a quoting
  3709.          character, then remember it as the delimiter. */
  3710.       if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1)
  3711.         delimiter = the_line[rl_point];
  3712.  
  3713.       /* If the character isn't needed to determine something special
  3714.          about what kind of completion to perform, then advance past it. */
  3715.  
  3716.       if (!rl_special_prefixes ||
  3717.           !rindex (rl_special_prefixes, the_line[rl_point]))
  3718.         rl_point++;
  3719.     }
  3720.     }
  3721.  
  3722.   start = rl_point;
  3723.   rl_point = end;
  3724.   text = rl_copy (start, end);
  3725.  
  3726.   /* If the user wants to TRY to complete, but then wants to give
  3727.      up and use the default completion function, they set the
  3728.      variable rl_attempted_completion_function. */
  3729.   if (rl_attempted_completion_function)
  3730.     {
  3731.       matches =
  3732.     (char **)(*rl_attempted_completion_function) (text, start, end);
  3733.  
  3734.       if (matches)
  3735.     {
  3736.       our_func = (Function *)NULL;
  3737.       goto after_usual_completion;
  3738.     }
  3739.     }
  3740.  
  3741.   matches = completion_matches (text, our_func);
  3742.  
  3743.  after_usual_completion:
  3744.   free (text);
  3745.  
  3746.   if (!matches)
  3747.     ding ();
  3748.   else
  3749.     {
  3750.       register int i;
  3751.  
  3752.     some_matches:
  3753.  
  3754.       /* It seems to me that in all the cases we handle we would like
  3755.      to ignore duplicate possiblilities.  Scan for the text to
  3756.      insert being identical to the other completions. */
  3757.       if (rl_ignore_completion_duplicates)
  3758.     {
  3759.       char *lowest_common;
  3760.       int j, newlen = 0;
  3761.  
  3762.       /* Sort the items. */
  3763.       /* It is safe to sort this array, because the lowest common
  3764.          denominator found in matches[0] will remain in place. */
  3765.       for (i = 0; matches[i]; i++);
  3766.       qsort (matches, i, sizeof (char *), compare_strings);
  3767.  
  3768.       /* Remember the lowest common denominator for it may be unique. */
  3769.       lowest_common = savestring (matches[0]);
  3770.  
  3771.       for (i = 0; matches[i + 1]; i++)
  3772.         {
  3773.           if (strcmp (matches[i], matches[i + 1]) == 0)
  3774.         {
  3775.           free (matches[i]);
  3776.           matches[i] = (char *)-1;
  3777.         }
  3778.           else
  3779.         newlen++;
  3780.         }
  3781.  
  3782.       /* We have marked all the dead slots with (char *)-1.
  3783.          Copy all the non-dead entries into a new array. */
  3784.       {
  3785.         char **temp_array =
  3786.           (char **)malloc ((3 + newlen) * sizeof (char *));
  3787.  
  3788.         for (i = 1, j = 1; matches[i]; i++)
  3789.           {
  3790.         if (matches[i] != (char *)-1)
  3791.           temp_array[j++] = matches[i];
  3792.           }
  3793.  
  3794.         temp_array[j] = (char *)NULL;
  3795.  
  3796.         if (matches[0] != (char *)-1)
  3797.           free (matches[0]);
  3798.  
  3799.         free (matches);
  3800.  
  3801.         matches = temp_array;
  3802.       }
  3803.  
  3804.       /* Place the lowest common denominator back in [0]. */
  3805.       matches[0] = lowest_common;
  3806.  
  3807.       /* If there is one string left, and it is identical to the
  3808.          lowest common denominator, then the LCD is the string to
  3809.          insert. */
  3810.       if (j == 2 && strcmp (matches[0], matches[1]) == 0)
  3811.         {
  3812.           free (matches[1]);
  3813.           matches[1] = (char *)NULL;
  3814.         }
  3815.     }
  3816.  
  3817.       switch (what_to_do)
  3818.     {
  3819.     case TAB:
  3820.       /* If we are matching filenames, then here is our chance to
  3821.          do clever processing by re-examining the list.  Call the
  3822.          ignore function with the array as a parameter.  It can
  3823.          munge the array, deleting matches as it desires. */
  3824.       if (rl_ignore_some_completions_function &&
  3825.           our_func == (int (*)())filename_completion_function)
  3826.         (void)(*rl_ignore_some_completions_function)(matches);
  3827.  
  3828.       if (matches[0])
  3829.         {
  3830.           rl_delete_text (start, rl_point);
  3831.           rl_point = start;
  3832.           rl_insert_text (matches[0]);
  3833.         }
  3834.  
  3835.       /* If there are more matches, ring the bell to indicate.
  3836.          If this was the only match, and we are hacking files,
  3837.          check the file to see if it was a directory.  If so,
  3838.          add a '/' to the name.  If not, and we are at the end
  3839.          of the line, then add a space. */
  3840.       if (matches[1])
  3841.         {
  3842.           ding ();        /* There are other matches remaining. */
  3843.         }
  3844.       else
  3845.         {
  3846.           char temp_string[2];
  3847.  
  3848.           temp_string[0] = delimiter ? delimiter : ' ';
  3849.           temp_string[1] = '\0';
  3850.  
  3851.           if (rl_filename_completion_desired)
  3852.         {
  3853.           struct stat finfo;
  3854.           char *filename = tilde_expand (matches[0]);
  3855.  
  3856.           if ((stat (filename, &finfo) == 0) &&
  3857.               S_ISDIR (finfo.st_mode))
  3858.             {
  3859.               if (the_line[rl_point] != '/')
  3860.             rl_insert_text ("/");
  3861.             }
  3862.           else
  3863.             {
  3864.               if (rl_point == rl_end)
  3865.             rl_insert_text (temp_string);
  3866.             }
  3867.           free (filename);
  3868.         }
  3869.           else
  3870.         {
  3871.           if (rl_point == rl_end)
  3872.             rl_insert_text (temp_string);
  3873.         }
  3874.         }
  3875.       break;
  3876.  
  3877.     case '*':
  3878.       {
  3879.         int i = 1;
  3880.  
  3881.         rl_delete_text (start, rl_point);
  3882.         rl_point = start;
  3883.         rl_begin_undo_group ();
  3884.         if (matches[1])
  3885.           {
  3886.         while (matches[i])
  3887.           {
  3888.             rl_insert_text (matches[i++]);
  3889.             rl_insert_text (" ");
  3890.           }
  3891.           }
  3892.         else
  3893.           {
  3894.         rl_insert_text (matches[0]);
  3895.         rl_insert_text (" ");
  3896.           }
  3897.         rl_end_undo_group ();
  3898.       }
  3899.       break;
  3900.  
  3901.     case '?':
  3902.       {
  3903.         int len, count, limit, max = 0;
  3904.         int j, k, l;
  3905.  
  3906.         /* Handle simple case first.  What if there is only one answer? */
  3907.         if (!matches[1])
  3908.           {
  3909.         char *temp;
  3910.  
  3911.         if (rl_filename_completion_desired)
  3912.           temp = rindex (matches[0], '/');
  3913.         else
  3914.           temp = (char *)NULL;
  3915.  
  3916.         if (!temp)
  3917.           temp = matches[0];
  3918.         else
  3919.           temp++;
  3920.  
  3921.         crlf ();
  3922.         fprintf (out_stream, "%s", temp);
  3923.         crlf ();
  3924.         goto restart;
  3925.           }
  3926.  
  3927.         /* There is more than one answer.  Find out how many there are,
  3928.            and find out what the maximum printed length of a single entry
  3929.            is. */
  3930.         for (i = 1; matches[i]; i++)
  3931.           {
  3932.         char *temp = (char *)NULL;
  3933.  
  3934.         /* If we are hacking filenames, then only count the characters
  3935.            after the last slash in the pathname. */
  3936.         if (rl_filename_completion_desired)
  3937.           temp = rindex (matches[i], '/');
  3938.         else
  3939.           temp = (char *)NULL;
  3940.  
  3941.         if (!temp)
  3942.           temp = matches[i];
  3943.         else
  3944.           temp++;
  3945.  
  3946.         if (strlen (temp) > max)
  3947.           max = strlen (temp);
  3948.           }
  3949.  
  3950.         len = i;
  3951.  
  3952.         /* If there are many items, then ask the user if she
  3953.            really wants to see them all. */
  3954.         if (len >= rl_completion_query_items)
  3955.           {
  3956.         crlf ();
  3957.         fprintf (out_stream,
  3958.              "There are %d possibilities.  Do you really", len);
  3959.         crlf ();
  3960.         fprintf (out_stream, "wish to see them all? (y or n)");
  3961.         fflush (out_stream);
  3962.         if (!get_y_or_n ())
  3963.           {
  3964.             crlf ();
  3965.             goto restart;
  3966.           }
  3967.           }
  3968.         /* How many items of MAX length can we fit in the screen window? */
  3969.         max += 2;
  3970.         limit = screenwidth / max;
  3971.         if (limit != 1 && (limit * max == screenwidth))
  3972.           limit--;
  3973.  
  3974.         /* Avoid a possible floating exception.  If max > screenwidth,
  3975.            limit will be 0 and a divide-by-zero fault will result. */
  3976.         if (limit == 0)
  3977.           limit = 1;
  3978.  
  3979.         /* How many iterations of the printing loop? */
  3980.         count = (len + (limit - 1)) / limit;
  3981.  
  3982.         /* Watch out for special case.  If LEN is less than LIMIT, then
  3983.            just do the inner printing loop. */
  3984.         if (len < limit) count = 1;
  3985.  
  3986.         /* Sort the items if they are not already sorted. */
  3987.         if (!rl_ignore_completion_duplicates)
  3988.           qsort (matches, len, sizeof (char *), compare_strings);
  3989.  
  3990.         /* Print the sorted items, up-and-down alphabetically, like
  3991.            ls might. */
  3992.         crlf ();
  3993.  
  3994.         for (i = 1; i < count + 1; i++)
  3995.           {
  3996.         for (j = 0, l = i; j < limit; j++)
  3997.           {
  3998.             if (l > len || !matches[l])
  3999.               {
  4000.             break;
  4001.               }
  4002.             else
  4003.               {
  4004.             char *temp = (char *)NULL;
  4005.  
  4006.             if (rl_filename_completion_desired)
  4007.               temp = rindex (matches[l], '/');
  4008.             else
  4009.               temp = (char *)NULL;
  4010.  
  4011.             if (!temp)
  4012.               temp = matches[l];
  4013.             else
  4014.               temp++;
  4015.  
  4016.             fprintf (out_stream, "%s", temp);
  4017.             for (k = 0; k < max - strlen (temp); k++)
  4018.               putc (' ', out_stream);
  4019.               }
  4020.             l += count;
  4021.           }
  4022.         crlf ();
  4023.           }
  4024.       restart:
  4025.  
  4026.         rl_on_new_line ();
  4027.       }
  4028.       break;
  4029.  
  4030.     default:
  4031.       abort ();
  4032.     }
  4033.  
  4034.       for (i = 0; matches[i]; i++)
  4035.     free (matches[i]);
  4036.       free (matches);
  4037.     }
  4038.  
  4039.   /* Check to see if the line has changed through all of this manipulation. */
  4040.   if (saved_line_buffer)
  4041.     {
  4042.       if (strcmp (the_line, saved_line_buffer) != 0)
  4043.     completion_changed_buffer = 1;
  4044.       else
  4045.     completion_changed_buffer = 0;
  4046.  
  4047.       free (saved_line_buffer);
  4048.     }
  4049. }
  4050.  
  4051. /* Stupid comparison routine for qsort () ing strings. */
  4052. static int
  4053. compare_strings (s1, s2)
  4054.   char **s1, **s2;
  4055. {
  4056.   return (strcmp (*s1, *s2));
  4057. }
  4058.  
  4059. /* A completion function for usernames.
  4060.    TEXT contains a partial username preceded by a random
  4061.    character (usually `~').  */
  4062. char *
  4063. username_completion_function (text, state)
  4064.      int state;
  4065.      char *text;
  4066. {
  4067. #ifdef __GO32__
  4068.   return (char *)NULL;
  4069. #else /* !__GO32__ */
  4070.   static char *username = (char *)NULL;
  4071.   static struct passwd *entry;
  4072.   static int namelen, first_char, first_char_loc;
  4073.  
  4074.   if (!state)
  4075.     {
  4076.       if (username)
  4077.     free (username);
  4078.  
  4079.       first_char = *text;
  4080.  
  4081.       if (first_char == '~')
  4082.     first_char_loc = 1;
  4083.       else
  4084.     first_char_loc = 0;
  4085.  
  4086.       username = savestring (&text[first_char_loc]);
  4087.       namelen = strlen (username);
  4088.       setpwent ();
  4089.     }
  4090.  
  4091.   while (entry = getpwent ())
  4092.     {
  4093.       if (strncmp (username, entry->pw_name, namelen) == 0)
  4094.     break;
  4095.     }
  4096.  
  4097.   if (!entry)
  4098.     {
  4099.       endpwent ();
  4100.       return ((char *)NULL);
  4101.     }
  4102.   else
  4103.     {
  4104.       char *value = (char *)xmalloc (2 + strlen (entry->pw_name));
  4105.  
  4106.       *value = *text;
  4107.  
  4108.       strcpy (value + first_char_loc, entry->pw_name);
  4109.  
  4110.       if (first_char == '~')
  4111.     rl_filename_completion_desired = 1;
  4112.  
  4113.       return (value);
  4114.     }
  4115. #endif /* !__GO32__ */
  4116. }
  4117.  
  4118. /* **************************************************************** */
  4119. /*                                    */
  4120. /*            Undo, and Undoing                */
  4121. /*                                    */
  4122. /* **************************************************************** */
  4123.  
  4124. /* Non-zero tells rl_delete_text and rl_insert_text to not add to
  4125.    the undo list. */
  4126. int doing_an_undo = 0;
  4127.  
  4128. /* The current undo list for THE_LINE. */
  4129. UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
  4130.  
  4131. /* Remember how to undo something.  Concatenate some undos if that
  4132.    seems right. */
  4133. rl_add_undo (what, start, end, text)
  4134.      enum undo_code what;
  4135.      int start, end;
  4136.      char *text;
  4137. {
  4138.   UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
  4139.   temp->what = what;
  4140.   temp->start = start;
  4141.   temp->end = end;
  4142.   temp->text = text;
  4143.   temp->next = rl_undo_list;
  4144.   rl_undo_list = temp;
  4145. }
  4146.  
  4147. /* Free the existing undo list. */
  4148. free_undo_list ()
  4149. {
  4150.   while (rl_undo_list) {
  4151.     UNDO_LIST *release = rl_undo_list;
  4152.     rl_undo_list = rl_undo_list->next;
  4153.  
  4154.     if (release->what == UNDO_DELETE)
  4155.       free (release->text);
  4156.  
  4157.     free (release);
  4158.   }
  4159. }
  4160.  
  4161. /* Undo the next thing in the list.  Return 0 if there
  4162.    is nothing to undo, or non-zero if there was. */
  4163. int
  4164. rl_do_undo ()
  4165. {
  4166.   UNDO_LIST *release;
  4167.   int waiting_for_begin = 0;
  4168.  
  4169. undo_thing:
  4170.   if (!rl_undo_list)
  4171.     return (0);
  4172.  
  4173.   doing_an_undo = 1;
  4174.  
  4175.   switch (rl_undo_list->what) {
  4176.  
  4177.     /* Undoing deletes means inserting some text. */
  4178.   case UNDO_DELETE:
  4179.     rl_point = rl_undo_list->start;
  4180.     rl_insert_text (rl_undo_list->text);
  4181.     free (rl_undo_list->text);
  4182.     break;
  4183.  
  4184.     /* Undoing inserts means deleting some text. */
  4185.   case UNDO_INSERT:
  4186.     rl_delete_text (rl_undo_list->start, rl_undo_list->end);
  4187.     rl_point = rl_undo_list->start;
  4188.     break;
  4189.  
  4190.     /* Undoing an END means undoing everything 'til we get to
  4191.        a BEGIN. */
  4192.   case UNDO_END:
  4193.     waiting_for_begin++;
  4194.     break;
  4195.  
  4196.     /* Undoing a BEGIN means that we are done with this group. */
  4197.   case UNDO_BEGIN:
  4198.     if (waiting_for_begin)
  4199.       waiting_for_begin--;
  4200.     else
  4201.       abort ();
  4202.     break;
  4203.   }
  4204.  
  4205.   doing_an_undo = 0;
  4206.  
  4207.   release = rl_undo_list;
  4208.   rl_undo_list = rl_undo_list->next;
  4209.   free (release);
  4210.  
  4211.   if (waiting_for_begin)
  4212.     goto undo_thing;
  4213.  
  4214.   return (1);
  4215. }
  4216.  
  4217. /* Begin a group.  Subsequent undos are undone as an atomic operation. */
  4218. rl_begin_undo_group ()
  4219. {
  4220.   rl_add_undo (UNDO_BEGIN, 0, 0, 0);
  4221. }
  4222.  
  4223. /* End an undo group started with rl_begin_undo_group (). */
  4224. rl_end_undo_group ()
  4225. {
  4226.   rl_add_undo (UNDO_END, 0, 0, 0);
  4227. }
  4228.  
  4229. /* Save an undo entry for the text from START to END. */
  4230. rl_modifying (start, end)
  4231.      int start, end;
  4232. {
  4233.   if (start > end)
  4234.     {
  4235.       int t = start;
  4236.       start = end;
  4237.       end = t;
  4238.     }
  4239.  
  4240.   if (start != end)
  4241.     {
  4242.       char *temp = rl_copy (start, end);
  4243.       rl_begin_undo_group ();
  4244.       rl_add_undo (UNDO_DELETE, start, end, temp);
  4245.       rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
  4246.       rl_end_undo_group ();
  4247.     }
  4248. }
  4249.  
  4250. /* Revert the current line to its previous state. */
  4251. rl_revert_line ()
  4252. {
  4253.   if (!rl_undo_list) ding ();
  4254.   else {
  4255.     while (rl_undo_list)
  4256.       rl_do_undo ();
  4257.   }
  4258. }
  4259.  
  4260. /* Do some undoing of things that were done. */
  4261. rl_undo_command (count)
  4262. {
  4263.   if (count < 0) return;    /* Nothing to do. */
  4264.  
  4265.   while (count)
  4266.     {
  4267.       if (rl_do_undo ())
  4268.     {
  4269.       count--;
  4270.     }
  4271.       else
  4272.     {
  4273.       ding ();
  4274.       break;
  4275.     }
  4276.     }
  4277. }
  4278.  
  4279. /* **************************************************************** */
  4280. /*                                    */
  4281. /*            History Utilities                */
  4282. /*                                    */
  4283. /* **************************************************************** */
  4284.  
  4285. /* We already have a history library, and that is what we use to control
  4286.    the history features of readline.  However, this is our local interface
  4287.    to the history mechanism. */
  4288.  
  4289. /* While we are editing the history, this is the saved
  4290.    version of the original line. */
  4291. HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
  4292.  
  4293. /* Set the history pointer back to the last entry in the history. */
  4294. start_using_history ()
  4295. {
  4296.   using_history ();
  4297.   if (saved_line_for_history)
  4298.     free_history_entry (saved_line_for_history);
  4299.  
  4300.   saved_line_for_history = (HIST_ENTRY *)NULL;
  4301. }
  4302.  
  4303. /* Free the contents (and containing structure) of a HIST_ENTRY. */
  4304. free_history_entry (entry)
  4305.      HIST_ENTRY *entry;
  4306. {
  4307.   if (!entry) return;
  4308.   if (entry->line)
  4309.     free (entry->line);
  4310.   free (entry);
  4311. }
  4312.  
  4313. /* Perhaps put back the current line if it has changed. */
  4314. maybe_replace_line ()
  4315. {
  4316.   HIST_ENTRY *temp = current_history ();
  4317.  
  4318.   /* If the current line has changed, save the changes. */
  4319.   if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
  4320.     {
  4321.       temp = replace_history_entry (where_history (), the_line, rl_undo_list);
  4322.       free (temp->line);
  4323.       free (temp);
  4324.     }
  4325. }
  4326.  
  4327. /* Put back the saved_line_for_history if there is one. */
  4328. maybe_unsave_line ()
  4329. {
  4330.   if (saved_line_for_history)
  4331.     {
  4332.       int line_len;
  4333.  
  4334.       line_len = strlen (saved_line_for_history->line);
  4335.  
  4336.       if (line_len >= rl_line_buffer_len)
  4337.     rl_extend_line_buffer (line_len);
  4338.  
  4339.       strcpy (the_line, saved_line_for_history->line);
  4340.       rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
  4341.       free_history_entry (saved_line_for_history);
  4342.       saved_line_for_history = (HIST_ENTRY *)NULL;
  4343.       rl_end = rl_point = strlen (the_line);
  4344.     }
  4345.   else
  4346.     ding ();
  4347. }
  4348.  
  4349. /* Save the current line in saved_line_for_history. */
  4350. maybe_save_line ()
  4351. {
  4352.   if (!saved_line_for_history)
  4353.     {
  4354.       saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  4355.       saved_line_for_history->line = savestring (the_line);
  4356.       saved_line_for_history->data = (char *)rl_undo_list;
  4357.     }
  4358. }
  4359.  
  4360. /* **************************************************************** */
  4361. /*                                    */
  4362. /*            History Commands                */
  4363. /*                                    */
  4364. /* **************************************************************** */
  4365.  
  4366. /* Meta-< goes to the start of the history. */
  4367. rl_beginning_of_history ()
  4368. {
  4369.   rl_get_previous_history (1 + where_history ());
  4370. }
  4371.  
  4372. /* Meta-> goes to the end of the history.  (The current line). */
  4373. rl_end_of_history ()
  4374. {
  4375.   maybe_replace_line ();
  4376.   using_history ();
  4377.   maybe_unsave_line ();
  4378. }
  4379.  
  4380. /* Move down to the next history line. */
  4381. rl_get_next_history (count)
  4382.      int count;
  4383. {
  4384.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  4385.  
  4386.   if (count < 0)
  4387.     {
  4388.       rl_get_previous_history (-count);
  4389.       return;
  4390.     }
  4391.  
  4392.   if (!count)
  4393.     return;
  4394.  
  4395.   maybe_replace_line ();
  4396.  
  4397.   while (count)
  4398.     {
  4399.       temp = next_history ();
  4400.       if (!temp)
  4401.     break;
  4402.       --count;
  4403.     }
  4404.  
  4405.   if (!temp)
  4406.     maybe_unsave_line ();
  4407.   else
  4408.     {
  4409.       int line_len;
  4410.  
  4411.       line_len = strlen (temp->line);
  4412.  
  4413.       if (line_len >= rl_line_buffer_len)
  4414.     rl_extend_line_buffer (line_len);
  4415.  
  4416.       strcpy (the_line, temp->line);
  4417.       rl_undo_list = (UNDO_LIST *)temp->data;
  4418.       rl_end = rl_point = strlen (the_line);
  4419. #if defined (VI_MODE)
  4420.       if (rl_editing_mode == vi_mode)
  4421.     rl_point = 0;
  4422. #endif /* VI_MODE */
  4423.     }
  4424. }
  4425.  
  4426. /* Get the previous item out of our interactive history, making it the current
  4427.    line.  If there is no previous history, just ding. */
  4428. rl_get_previous_history (count)
  4429.      int count;
  4430. {
  4431.   HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
  4432.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  4433.  
  4434.   if (count < 0)
  4435.     {
  4436.       rl_get_next_history (-count);
  4437.       return;
  4438.     }
  4439.  
  4440.   if (!count)
  4441.     return;
  4442.  
  4443.   /* If we don't have a line saved, then save this one. */
  4444.   maybe_save_line ();
  4445.  
  4446.   /* If the current line has changed, save the changes. */
  4447.   maybe_replace_line ();
  4448.  
  4449.   while (count)
  4450.     {
  4451.       temp = previous_history ();
  4452.       if (!temp)
  4453.     break;
  4454.       else
  4455.     old_temp = temp;
  4456.       --count;
  4457.     }
  4458.  
  4459.   /* If there was a large argument, and we moved back to the start of the
  4460.      history, that is not an error.  So use the last value found. */
  4461.   if (!temp && old_temp)
  4462.     temp = old_temp;
  4463.  
  4464.   if (!temp)
  4465.     ding ();
  4466.   else
  4467.     {
  4468.       int line_len;
  4469.  
  4470.       line_len = strlen (temp->line);
  4471.  
  4472.       if (line_len >= rl_line_buffer_len)
  4473.     rl_extend_line_buffer (line_len);
  4474.  
  4475.       strcpy (the_line, temp->line);
  4476.       rl_undo_list = (UNDO_LIST *)temp->data;
  4477.       rl_end = rl_point = line_len;
  4478.  
  4479. #if defined (VI_MODE)
  4480.       if (rl_editing_mode == vi_mode)
  4481.     rl_point = 0;
  4482. #endif /* VI_MODE */
  4483.     }
  4484. }
  4485.  
  4486.  
  4487. /* **************************************************************** */
  4488. /*                                    */
  4489. /*            I-Search and Searching                */
  4490. /*                                    */
  4491. /* **************************************************************** */
  4492.  
  4493. /* Search backwards through the history looking for a string which is typed
  4494.    interactively.  Start with the current line. */
  4495. rl_reverse_search_history (sign, key)
  4496.      int sign;
  4497.      int key;
  4498. {
  4499.   rl_search_history (-sign, key);
  4500. }
  4501.  
  4502. /* Search forwards through the history looking for a string which is typed
  4503.    interactively.  Start with the current line. */
  4504. rl_forward_search_history (sign, key)
  4505.      int sign;
  4506.      int key;
  4507. {
  4508.   rl_search_history (sign, key);
  4509. }
  4510.  
  4511. /* Display the current state of the search in the echo-area.
  4512.    SEARCH_STRING contains the string that is being searched for,
  4513.    DIRECTION is zero for forward, or 1 for reverse,
  4514.    WHERE is the history list number of the current line.  If it is
  4515.    -1, then this line is the starting one. */
  4516. rl_display_search (search_string, reverse_p, where)
  4517.      char *search_string;
  4518.      int reverse_p, where;
  4519. {
  4520.   char *message = (char *)NULL;
  4521.  
  4522.   message =
  4523.     (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30);
  4524.  
  4525.   *message = '\0';
  4526.  
  4527. #if defined (NOTDEF)
  4528.   if (where != -1)
  4529.     sprintf (message, "[%d]", where + history_base);
  4530. #endif /* NOTDEF */
  4531.  
  4532.   strcat (message, "(");
  4533.  
  4534.   if (reverse_p)
  4535.     strcat (message, "reverse-");
  4536.  
  4537.   strcat (message, "i-search)`");
  4538.  
  4539.   if (search_string)
  4540.     strcat (message, search_string);
  4541.  
  4542.   strcat (message, "': ");
  4543.   rl_message (message, 0, 0);
  4544.   rl_redisplay ();
  4545. }
  4546.  
  4547. /* Search through the history looking for an interactively typed string.
  4548.    This is analogous to i-search.  We start the search in the current line.
  4549.    DIRECTION is which direction to search; >= 0 means forward, < 0 means
  4550.    backwards. */
  4551. rl_search_history (direction, invoking_key)
  4552.      int direction;
  4553.      int invoking_key;
  4554. {
  4555.   /* The string that the user types in to search for. */
  4556.   char *search_string = (char *)alloca (128);
  4557.  
  4558.   /* The current length of SEARCH_STRING. */
  4559.   int search_string_index;
  4560.  
  4561.   /* The list of lines to search through. */
  4562.   char **lines;
  4563.  
  4564.   /* The length of LINES. */
  4565.   int hlen;
  4566.  
  4567.   /* Where we get LINES from. */
  4568.   HIST_ENTRY **hlist = history_list ();
  4569.  
  4570.   register int i = 0;
  4571.   int orig_point = rl_point;
  4572.   int orig_line = where_history ();
  4573.   int last_found_line = orig_line;
  4574.   int c, done = 0;
  4575.  
  4576.   /* The line currently being searched. */
  4577.   char *sline;
  4578.  
  4579.   /* Offset in that line. */
  4580.   int index;
  4581.  
  4582.   /* Non-zero if we are doing a reverse search. */
  4583.   int reverse = (direction < 0);
  4584.  
  4585.   /* Create an arrary of pointers to the lines that we want to search. */
  4586.   maybe_replace_line ();
  4587.   if (hlist)
  4588.     for (i = 0; hlist[i]; i++);
  4589.  
  4590.   /* Allocate space for this many lines, +1 for the current input line,
  4591.      and remember those lines. */
  4592.   lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
  4593.   for (i = 0; i < hlen; i++)
  4594.     lines[i] = hlist[i]->line;
  4595.  
  4596.   if (saved_line_for_history)
  4597.     lines[i] = saved_line_for_history->line;
  4598.   else
  4599.     /* So I have to type it in this way instead. */
  4600.     {
  4601.       char *alloced_line;
  4602.  
  4603.       /* Keep that mips alloca happy. */
  4604.       alloced_line = (char *)alloca (1 + strlen (the_line));
  4605.       lines[i] = alloced_line;
  4606.       strcpy (lines[i], &the_line[0]);
  4607.     }
  4608.  
  4609.   hlen++;
  4610.  
  4611.   /* The line where we start the search. */
  4612.   i = orig_line;
  4613.  
  4614.   /* Initialize search parameters. */
  4615.   *search_string = '\0';
  4616.   search_string_index = 0;
  4617.  
  4618.   /* Normalize DIRECTION into 1 or -1. */
  4619.   if (direction >= 0)
  4620.     direction = 1;
  4621.   else
  4622.     direction = -1;
  4623.  
  4624.   rl_display_search (search_string, reverse, -1);
  4625.  
  4626.   sline = the_line;
  4627.   index = rl_point;
  4628.  
  4629.   while (!done)
  4630.     {
  4631.       c = rl_read_key ();
  4632.  
  4633.       /* Hack C to Do What I Mean. */
  4634.       {
  4635.     Function *f = (Function *)NULL;
  4636.  
  4637.     if (keymap[c].type == ISFUNC)
  4638.       {
  4639.         f = keymap[c].function;
  4640.  
  4641.         if (f == rl_reverse_search_history)
  4642.           c = reverse ? -1 : -2;
  4643.         else if (f == rl_forward_search_history)
  4644.           c =  !reverse ? -1 : -2;
  4645.       }
  4646.       }
  4647.  
  4648.       switch (c)
  4649.     {
  4650.     case ESC:
  4651.       done = 1;
  4652.       continue;
  4653.  
  4654.       /* case invoking_key: */
  4655.     case -1:
  4656.       goto search_again;
  4657.  
  4658.       /* switch directions */
  4659.     case -2:
  4660.       direction = -direction;
  4661.       reverse = (direction < 0);
  4662.  
  4663.       goto do_search;
  4664.  
  4665.     case CTRL ('G'):
  4666.       strcpy (the_line, lines[orig_line]);
  4667.       rl_point = orig_point;
  4668.       rl_end = strlen (the_line);
  4669.       rl_clear_message ();
  4670.       return;
  4671.  
  4672.     default:
  4673.       if (c < 32 || c > 126)
  4674.         {
  4675.           rl_execute_next (c);
  4676.           done = 1;
  4677.           continue;
  4678.         }
  4679.       else
  4680.         {
  4681.           search_string[search_string_index++] = c;
  4682.           search_string[search_string_index] = '\0';
  4683.           goto do_search;
  4684.  
  4685.         search_again:
  4686.  
  4687.           if (!search_string_index)
  4688.         continue;
  4689.           else
  4690.         {
  4691.           if (reverse)
  4692.             --index;
  4693.           else
  4694.             if (index != strlen (sline))
  4695.               ++index;
  4696.             else
  4697.               ding ();
  4698.         }
  4699.         do_search:
  4700.  
  4701.           while (1)
  4702.         {
  4703.           if (reverse)
  4704.             {
  4705.               while (index >= 0)
  4706.             if (strncmp
  4707.                 (search_string, sline + index, search_string_index)
  4708.                 == 0)
  4709.               goto string_found;
  4710.             else
  4711.               index--;
  4712.             }
  4713.           else
  4714.             {
  4715.               register int limit =
  4716.             (strlen (sline) - search_string_index) + 1;
  4717.  
  4718.               while (index < limit)
  4719.             {
  4720.               if (strncmp (search_string,
  4721.                        sline + index,
  4722.                        search_string_index) == 0)
  4723.                 goto string_found;
  4724.               index++;
  4725.             }
  4726.             }
  4727.  
  4728.         next_line:
  4729.           i += direction;
  4730.  
  4731.           /* At limit for direction? */
  4732.           if ((reverse && i < 0) ||
  4733.               (!reverse && i == hlen))
  4734.             goto search_failed;
  4735.  
  4736.           sline = lines[i];
  4737.           if (reverse)
  4738.             index = strlen (sline);
  4739.           else
  4740.             index = 0;
  4741.  
  4742.           /* If the search string is longer than the current
  4743.              line, no match. */
  4744.           if (search_string_index > strlen (sline))
  4745.             goto next_line;
  4746.  
  4747.           /* Start actually searching. */
  4748.           if (reverse)
  4749.             index -= search_string_index;
  4750.         }
  4751.  
  4752.         search_failed:
  4753.           /* We cannot find the search string.  Ding the bell. */
  4754.           ding ();
  4755.           i = last_found_line;
  4756.           break;
  4757.  
  4758.         string_found:
  4759.           /* We have found the search string.  Just display it.  But don't
  4760.          actually move there in the history list until the user accepts
  4761.          the location. */
  4762.           {
  4763.         int line_len;
  4764.  
  4765.         line_len = strlen (lines[i]);
  4766.  
  4767.         if (line_len >= rl_line_buffer_len)
  4768.           rl_extend_line_buffer (line_len);
  4769.  
  4770.         strcpy (the_line, lines[i]);
  4771.         rl_point = index;
  4772.         rl_end = line_len;
  4773.         last_found_line = i;
  4774.         rl_display_search
  4775.           (search_string, reverse, (i == orig_line) ? -1 : i);
  4776.           }
  4777.         }
  4778.     }
  4779.       continue;
  4780.     }
  4781.  
  4782.   /* The searching is over.  The user may have found the string that she
  4783.      was looking for, or else she may have exited a failing search.  If
  4784.      INDEX is -1, then that shows that the string searched for was not
  4785.      found.  We use this to determine where to place rl_point. */
  4786.   {
  4787.     int now = last_found_line;
  4788.  
  4789.     /* First put back the original state. */
  4790.     strcpy (the_line, lines[orig_line]);
  4791.  
  4792.     if (now < orig_line)
  4793.       rl_get_previous_history (orig_line - now);
  4794.     else
  4795.       rl_get_next_history (now - orig_line);
  4796.  
  4797.     /* If the index of the "matched" string is less than zero, then the
  4798.        final search string was never matched, so put point somewhere
  4799.        reasonable. */
  4800.     if (index < 0)
  4801.       index = strlen (the_line);
  4802.  
  4803.     rl_point = index;
  4804.     rl_clear_message ();
  4805.   }
  4806. }
  4807.  
  4808. /* Make C be the next command to be executed. */
  4809. rl_execute_next (c)
  4810.      int c;
  4811. {
  4812.   rl_pending_input = c;
  4813. }
  4814.  
  4815. /* **************************************************************** */
  4816. /*                                    */
  4817. /*            Killing Mechanism                */
  4818. /*                                    */
  4819. /* **************************************************************** */
  4820.  
  4821. /* What we assume for a max number of kills. */
  4822. #define DEFAULT_MAX_KILLS 10
  4823.  
  4824. /* The real variable to look at to find out when to flush kills. */
  4825. int rl_max_kills = DEFAULT_MAX_KILLS;
  4826.  
  4827. /* Where to store killed text. */
  4828. char **rl_kill_ring = (char **)NULL;
  4829.  
  4830. /* Where we are in the kill ring. */
  4831. int rl_kill_index = 0;
  4832.  
  4833. /* How many slots we have in the kill ring. */
  4834. int rl_kill_ring_length = 0;
  4835.  
  4836. /* How to say that you only want to save a certain amount
  4837.    of kill material. */
  4838. rl_set_retained_kills (num)
  4839.      int num;
  4840. {}
  4841.  
  4842. /* The way to kill something.  This appends or prepends to the last
  4843.    kill, if the last command was a kill command.  if FROM is less
  4844.    than TO, then the text is appended, otherwise prepended.  If the
  4845.    last command was not a kill command, then a new slot is made for
  4846.    this kill. */
  4847. rl_kill_text (from, to)
  4848.      int from, to;
  4849. {
  4850.   int slot;
  4851.   char *text = rl_copy (from, to);
  4852.  
  4853.   /* Is there anything to kill? */
  4854.   if (from == to)
  4855.     {
  4856.       free (text);
  4857.       last_command_was_kill++;
  4858.       return;
  4859.     }
  4860.  
  4861.   /* Delete the copied text from the line. */
  4862.   rl_delete_text (from, to);
  4863.  
  4864.   /* First, find the slot to work with. */
  4865.   if (!last_command_was_kill)
  4866.     {
  4867.       /* Get a new slot.  */
  4868.       if (!rl_kill_ring)
  4869.     {
  4870.       /* If we don't have any defined, then make one. */
  4871.       rl_kill_ring = (char **)
  4872.         xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
  4873.       slot = 1;
  4874.     }
  4875.       else
  4876.     {
  4877.       /* We have to add a new slot on the end, unless we have
  4878.          exceeded the max limit for remembering kills. */
  4879.       slot = rl_kill_ring_length;
  4880.       if (slot == rl_max_kills)
  4881.         {
  4882.           register int i;
  4883.           free (rl_kill_ring[0]);
  4884.           for (i = 0; i < slot; i++)
  4885.         rl_kill_ring[i] = rl_kill_ring[i + 1];
  4886.         }
  4887.       else
  4888.         {
  4889.           rl_kill_ring =
  4890.         (char **)
  4891.           xrealloc (rl_kill_ring,
  4892.                 ((slot = (rl_kill_ring_length += 1)) + 1)
  4893.                 * sizeof (char *));
  4894.         }
  4895.     }
  4896.       slot--;
  4897.     }
  4898.   else
  4899.     {
  4900.       slot = rl_kill_ring_length - 1;
  4901.     }
  4902.  
  4903.   /* If the last command was a kill, prepend or append. */
  4904.   if (last_command_was_kill && rl_editing_mode != vi_mode)
  4905.     {
  4906.       char *old = rl_kill_ring[slot];
  4907.       char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
  4908.  
  4909.       if (from < to)
  4910.     {
  4911.       strcpy (new, old);
  4912.       strcat (new, text);
  4913.     }
  4914.       else
  4915.     {
  4916.       strcpy (new, text);
  4917.       strcat (new, old);
  4918.     }
  4919.       free (old);
  4920.       free (text);
  4921.       rl_kill_ring[slot] = new;
  4922.     }
  4923.   else
  4924.     {
  4925.       rl_kill_ring[slot] = text;
  4926.     }
  4927.   rl_kill_index = slot;
  4928.   last_command_was_kill++;
  4929. }
  4930.  
  4931. /* Now REMEMBER!  In order to do prepending or appending correctly, kill
  4932.    commands always make rl_point's original position be the FROM argument,
  4933.    and rl_point's extent be the TO argument. */
  4934.  
  4935. /* **************************************************************** */
  4936. /*                                    */
  4937. /*            Killing Commands                */
  4938. /*                                    */
  4939. /* **************************************************************** */
  4940.  
  4941. /* Delete the word at point, saving the text in the kill ring. */
  4942. rl_kill_word (count)
  4943.      int count;
  4944. {
  4945.   int orig_point = rl_point;
  4946.  
  4947.   if (count < 0)
  4948.     rl_backward_kill_word (-count);
  4949.   else
  4950.     {
  4951.       rl_forward_word (count);
  4952.  
  4953.       if (rl_point != orig_point)
  4954.     rl_kill_text (orig_point, rl_point);
  4955.  
  4956.       rl_point = orig_point;
  4957.     }
  4958. }
  4959.  
  4960. /* Rubout the word before point, placing it on the kill ring. */
  4961. rl_backward_kill_word (count)
  4962.      int count;
  4963. {
  4964.   int orig_point = rl_point;
  4965.  
  4966.   if (count < 0)
  4967.     rl_kill_word (-count);
  4968.   else
  4969.     {
  4970.       rl_backward_word (count);
  4971.  
  4972.       if (rl_point != orig_point)
  4973.     rl_kill_text (orig_point, rl_point);
  4974.     }
  4975. }
  4976.  
  4977. /* Kill from here to the end of the line.  If DIRECTION is negative, kill
  4978.    back to the line start instead. */
  4979. rl_kill_line (direction)
  4980.      int direction;
  4981. {
  4982.   int orig_point = rl_point;
  4983.  
  4984.   if (direction < 0)
  4985.     rl_backward_kill_line (1);
  4986.   else
  4987.     {
  4988.       rl_end_of_line ();
  4989.       if (orig_point != rl_point)
  4990.     rl_kill_text (orig_point, rl_point);
  4991.       rl_point = orig_point;
  4992.     }
  4993. }
  4994.  
  4995. /* Kill backwards to the start of the line.  If DIRECTION is negative, kill
  4996.    forwards to the line end instead. */
  4997. rl_backward_kill_line (direction)
  4998.      int direction;
  4999. {
  5000.   int orig_point = rl_point;
  5001.  
  5002.   if (direction < 0)
  5003.     rl_kill_line (1);
  5004.   else
  5005.     {
  5006.       if (!rl_point)
  5007.     ding ();
  5008.       else
  5009.     {
  5010.       rl_beg_of_line ();
  5011.       rl_kill_text (orig_point, rl_point);
  5012.     }
  5013.     }
  5014. }
  5015.  
  5016. /* Yank back the last killed text.  This ignores arguments. */
  5017. rl_yank ()
  5018. {
  5019.   if (!rl_kill_ring) rl_abort ();
  5020.   rl_insert_text (rl_kill_ring[rl_kill_index]);
  5021. }
  5022.  
  5023. /* If the last command was yank, or yank_pop, and the text just
  5024.    before point is identical to the current kill item, then
  5025.    delete that text from the line, rotate the index down, and
  5026.    yank back some other text. */
  5027. rl_yank_pop ()
  5028. {
  5029.   int l;
  5030.  
  5031.   if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
  5032.       !rl_kill_ring)
  5033.     {
  5034.       rl_abort ();
  5035.     }
  5036.  
  5037.   l = strlen (rl_kill_ring[rl_kill_index]);
  5038.   if (((rl_point - l) >= 0) &&
  5039.       (strncmp (the_line + (rl_point - l),
  5040.         rl_kill_ring[rl_kill_index], l) == 0))
  5041.     {
  5042.       rl_delete_text ((rl_point - l), rl_point);
  5043.       rl_point -= l;
  5044.       rl_kill_index--;
  5045.       if (rl_kill_index < 0)
  5046.     rl_kill_index = rl_kill_ring_length - 1;
  5047.       rl_yank ();
  5048.     }
  5049.   else
  5050.     rl_abort ();
  5051.  
  5052. }
  5053.  
  5054. /* Yank the COUNTth argument from the previous history line. */
  5055. rl_yank_nth_arg (count, ignore)
  5056.      int count;
  5057. {
  5058.   register HIST_ENTRY *entry = previous_history ();
  5059.   char *arg;
  5060.  
  5061.   if (entry)
  5062.     next_history ();
  5063.   else
  5064.     {
  5065.       ding ();
  5066.       return;
  5067.     }
  5068.  
  5069.   arg = history_arg_extract (count, count, entry->line);
  5070.   if (!arg || !*arg)
  5071.     {
  5072.       ding ();
  5073.       return;
  5074.     }
  5075.  
  5076.   rl_begin_undo_group ();
  5077.  
  5078. #if defined (VI_MODE)
  5079.   /* Vi mode always inserts a space befoe yanking the argument, and it
  5080.      inserts it right *after* rl_point. */
  5081.   if (rl_editing_mode == vi_mode)
  5082.     rl_point++;
  5083. #endif /* VI_MODE */
  5084.  
  5085.   if (rl_point && the_line[rl_point - 1] != ' ')
  5086.     rl_insert_text (" ");
  5087.  
  5088.   rl_insert_text (arg);
  5089.   free (arg);
  5090.  
  5091.   rl_end_undo_group ();
  5092. }
  5093.  
  5094. /* How to toggle back and forth between editing modes. */
  5095. rl_vi_editing_mode ()
  5096. {
  5097. #if defined (VI_MODE)
  5098.   rl_editing_mode = vi_mode;
  5099.   rl_vi_insertion_mode ();
  5100. #endif /* VI_MODE */
  5101. }
  5102.  
  5103. rl_emacs_editing_mode ()
  5104. {
  5105.   rl_editing_mode = emacs_mode;
  5106.   keymap = emacs_standard_keymap;
  5107. }
  5108.  
  5109.  
  5110. /* **************************************************************** */
  5111. /*                                    */
  5112. /*                 Completion                    */
  5113. /*                                    */
  5114. /* **************************************************************** */
  5115.  
  5116. /* Non-zero means that case is not significant in completion. */
  5117. int completion_case_fold = 0;
  5118.  
  5119. /* Return an array of (char *) which is a list of completions for TEXT.
  5120.    If there are no completions, return a NULL pointer.
  5121.    The first entry in the returned array is the substitution for TEXT.
  5122.    The remaining entries are the possible completions.
  5123.    The array is terminated with a NULL pointer.
  5124.  
  5125.    ENTRY_FUNCTION is a function of two args, and returns a (char *).
  5126.      The first argument is TEXT.
  5127.      The second is a state argument; it should be zero on the first call, and
  5128.      non-zero on subsequent calls.  It returns a NULL pointer to the caller
  5129.      when there are no more matches.
  5130.  */
  5131. char **
  5132. completion_matches (text, entry_function)
  5133.      char *text;
  5134.      char *(*entry_function) ();
  5135. {
  5136.   /* Number of slots in match_list. */
  5137.   int match_list_size;
  5138.  
  5139.   /* The list of matches. */
  5140.   char **match_list =
  5141.     (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
  5142.  
  5143.   /* Number of matches actually found. */
  5144.   int matches = 0;
  5145.  
  5146.   /* Temporary string binder. */
  5147.   char *string;
  5148.  
  5149.   match_list[1] = (char *)NULL;
  5150.  
  5151.   while (string = (*entry_function) (text, matches))
  5152.     {
  5153.       if (matches + 1 == match_list_size)
  5154.     match_list = (char **)xrealloc
  5155.       (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
  5156.  
  5157.       match_list[++matches] = string;
  5158.       match_list[matches + 1] = (char *)NULL;
  5159.     }
  5160.  
  5161.   /* If there were any matches, then look through them finding out the
  5162.      lowest common denominator.  That then becomes match_list[0]. */
  5163.   if (matches)
  5164.     {
  5165.       register int i = 1;
  5166.       int low = 100000;        /* Count of max-matched characters. */
  5167.  
  5168.       /* If only one match, just use that. */
  5169.       if (matches == 1)
  5170.     {
  5171.       match_list[0] = match_list[1];
  5172.       match_list[1] = (char *)NULL;
  5173.     }
  5174.       else
  5175.     {
  5176.       /* Otherwise, compare each member of the list with
  5177.          the next, finding out where they stop matching. */
  5178.  
  5179.       while (i < matches)
  5180.         {
  5181.           register int c1, c2, si;
  5182.  
  5183.           if (completion_case_fold)
  5184.         {
  5185.           for (si = 0;
  5186.                (c1 = to_lower(match_list[i][si])) &&
  5187.                (c2 = to_lower(match_list[i + 1][si]));
  5188.                si++)
  5189.             if (c1 != c2) break;
  5190.         }
  5191.           else
  5192.         {
  5193.           for (si = 0;
  5194.                (c1 = match_list[i][si]) &&
  5195.                (c2 = match_list[i + 1][si]);
  5196.                si++)
  5197.             if (c1 != c2) break;
  5198.         }
  5199.  
  5200.           if (low > si) low = si;
  5201.           i++;
  5202.         }
  5203.       match_list[0] = (char *)xmalloc (low + 1);
  5204.       strncpy (match_list[0], match_list[1], low);
  5205.       match_list[0][low] = '\0';
  5206.     }
  5207.     }
  5208.   else                /* There were no matches. */
  5209.     {
  5210.       free (match_list);
  5211.       match_list = (char **)NULL;
  5212.     }
  5213.   return (match_list);
  5214. }
  5215.  
  5216. /* Okay, now we write the entry_function for filename completion.  In the
  5217.    general case.  Note that completion in the shell is a little different
  5218.    because of all the pathnames that must be followed when looking up the
  5219.    completion for a command. */
  5220. char *
  5221. filename_completion_function (text, state)
  5222.      int state;
  5223.      char *text;
  5224. {
  5225.   static DIR *directory;
  5226.   static char *filename = (char *)NULL;
  5227.   static char *dirname = (char *)NULL;
  5228.   static char *users_dirname = (char *)NULL;
  5229.   static int filename_len;
  5230.  
  5231.   dirent *entry = (dirent *)NULL;
  5232.  
  5233.   /* If we don't have any state, then do some initialization. */
  5234.   if (!state)
  5235.     {
  5236.       char *temp;
  5237.  
  5238.       if (dirname) free (dirname);
  5239.       if (filename) free (filename);
  5240.       if (users_dirname) free (users_dirname);
  5241.  
  5242.       filename = savestring (text);
  5243.       if (!*text) text = ".";
  5244.       dirname = savestring (text);
  5245.  
  5246.       temp = rindex (dirname, '/');
  5247.  
  5248.       if (temp)
  5249.     {
  5250.       strcpy (filename, ++temp);
  5251.       *temp = '\0';
  5252.     }
  5253.       else
  5254.     strcpy (dirname, ".");
  5255.  
  5256.       /* We aren't done yet.  We also support the "~user" syntax. */
  5257.  
  5258.       /* Save the version of the directory that the user typed. */
  5259.       users_dirname = savestring (dirname);
  5260.       {
  5261.     char *temp_dirname;
  5262.  
  5263.     temp_dirname = tilde_expand (dirname);
  5264.     free (dirname);
  5265.     dirname = temp_dirname;
  5266.  
  5267.     if (rl_symbolic_link_hook)
  5268.       (*rl_symbolic_link_hook) (&dirname);
  5269.       }
  5270.       directory = opendir (dirname);
  5271.       filename_len = strlen (filename);
  5272.  
  5273.       rl_filename_completion_desired = 1;
  5274.     }
  5275.  
  5276.   /* At this point we should entertain the possibility of hacking wildcarded
  5277.      filenames, like /usr/man/man<WILD>/te<TAB>.  If the directory name
  5278.      contains globbing characters, then build an array of directories to
  5279.      glob on, and glob on the first one. */
  5280.  
  5281.   /* Now that we have some state, we can read the directory. */
  5282.  
  5283.   while (directory && (entry = readdir (directory)))
  5284.     {
  5285.       /* Special case for no filename.
  5286.      All entries except "." and ".." match. */
  5287.       if (!filename_len)
  5288.     {
  5289.       if ((strcmp (entry->d_name, ".") != 0) &&
  5290.           (strcmp (entry->d_name, "..") != 0))
  5291.         break;
  5292.     }
  5293.       else
  5294.     {
  5295.       /* Otherwise, if these match upto the length of filename, then
  5296.          it is a match. */
  5297.         if (entry->d_name[0] == filename[0] && /* Quick test */
  5298.         (strncmp (filename, entry->d_name, filename_len) == 0))
  5299.           {
  5300.         break;
  5301.           }
  5302.     }
  5303.     }
  5304.  
  5305.   if (!entry)
  5306.     {
  5307.       if (directory)
  5308.     {
  5309.       closedir (directory);
  5310.       directory = (DIR *)NULL;
  5311.     }
  5312.       return (char *)NULL;
  5313.     }
  5314.   else
  5315.     {
  5316.       char *temp;
  5317.  
  5318.       if (dirname && (strcmp (dirname, ".") != 0))
  5319.     {
  5320.       temp = (char *)
  5321.         xmalloc (1 + strlen (users_dirname) + strlen (entry->d_name));
  5322.       strcpy (temp, users_dirname);
  5323.       strcat (temp, entry->d_name);
  5324.     }
  5325.       else
  5326.     {
  5327.       temp = (savestring (entry->d_name));
  5328.     }
  5329.       return (temp);
  5330.     }
  5331. }
  5332.  
  5333.  
  5334. /* **************************************************************** */
  5335. /*                                    */
  5336. /*            Binding keys                    */
  5337. /*                                    */
  5338. /* **************************************************************** */
  5339.  
  5340. /* rl_add_defun (char *name, Function *function, int key)
  5341.    Add NAME to the list of named functions.  Make FUNCTION
  5342.    be the function that gets called.
  5343.    If KEY is not -1, then bind it. */
  5344. rl_add_defun (name, function, key)
  5345.      char *name;
  5346.      Function *function;
  5347.      int key;
  5348. {
  5349.   if (key != -1)
  5350.     rl_bind_key (key, function);
  5351.   rl_add_funmap_entry (name, function);
  5352. }
  5353.  
  5354. /* Bind KEY to FUNCTION.  Returns non-zero if KEY is out of range. */
  5355. int
  5356. rl_bind_key (key, function)
  5357.      int key;
  5358.      Function *function;
  5359. {
  5360.   if (key < 0)
  5361.     return (key);
  5362.  
  5363.   if (key > 127 && key < 256)
  5364.     {
  5365.       if (keymap[ESC].type == ISKMAP)
  5366.     {
  5367.       Keymap escmap = (Keymap)keymap[ESC].function;
  5368.  
  5369.       key -= 128;
  5370.       escmap[key].type = ISFUNC;
  5371.       escmap[key].function = function;
  5372.       return (0);
  5373.     }
  5374.       return (key);
  5375.     }
  5376.  
  5377.   keymap[key].type = ISFUNC;
  5378.   keymap[key].function = function;
  5379.  return (0);
  5380. }
  5381.  
  5382. /* Bind KEY to FUNCTION in MAP.  Returns non-zero in case of invalid
  5383.    KEY. */
  5384. int
  5385. rl_bind_key_in_map (key, function, map)
  5386.      int key;
  5387.      Function *function;
  5388.      Keymap map;
  5389. {
  5390.   int result;
  5391.   Keymap oldmap = keymap;
  5392.  
  5393.   keymap = map;
  5394.   result = rl_bind_key (key, function);
  5395.   keymap = oldmap;
  5396.   return (result);
  5397. }
  5398.  
  5399. /* Make KEY do nothing in the currently selected keymap.
  5400.    Returns non-zero in case of error. */
  5401. int
  5402. rl_unbind_key (key)
  5403.      int key;
  5404. {
  5405.   return (rl_bind_key (key, (Function *)NULL));
  5406. }
  5407.  
  5408. /* Make KEY do nothing in MAP.
  5409.    Returns non-zero in case of error. */
  5410. int
  5411. rl_unbind_key_in_map (key, map)
  5412.      int key;
  5413.      Keymap map;
  5414. {
  5415.   return (rl_bind_key_in_map (key, (Function *)NULL, map));
  5416. }
  5417.  
  5418. /* Bind the key sequence represented by the string KEYSEQ to
  5419.    FUNCTION.  This makes new keymaps as necessary.  The initial
  5420.    place to do bindings is in MAP. */
  5421. rl_set_key (keyseq, function, map)
  5422.      char *keyseq;
  5423.      Function *function;
  5424.      Keymap map;
  5425. {
  5426.   rl_generic_bind (ISFUNC, keyseq, function, map);
  5427. }
  5428.  
  5429. /* Bind the key sequence represented by the string KEYSEQ to
  5430.    the string of characters MACRO.  This makes new keymaps as
  5431.    necessary.  The initial place to do bindings is in MAP. */
  5432. rl_macro_bind (keyseq, macro, map)
  5433.      char *keyseq, *macro;
  5434.      Keymap map;
  5435. {
  5436.   char *macro_keys;
  5437.   int macro_keys_len;
  5438.  
  5439.   macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
  5440.  
  5441.   if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
  5442.     {
  5443.       free (macro_keys);
  5444.       return;
  5445.     }
  5446.   rl_generic_bind (ISMACR, keyseq, macro_keys, map);
  5447. }
  5448.  
  5449. /* Bind the key sequence represented by the string KEYSEQ to
  5450.    the arbitrary pointer DATA.  TYPE says what kind of data is
  5451.    pointed to by DATA, right now this can be a function (ISFUNC),
  5452.    a macro (ISMACR), or a keymap (ISKMAP).  This makes new keymaps
  5453.    as necessary.  The initial place to do bindings is in MAP. */
  5454. rl_generic_bind (type, keyseq, data, map)
  5455.      int type;
  5456.      char *keyseq, *data;
  5457.      Keymap map;
  5458. {
  5459.   char *keys;
  5460.   int keys_len;
  5461.   register int i;
  5462.  
  5463.   /* If no keys to bind to, exit right away. */
  5464.   if (!keyseq || !*keyseq)
  5465.     {
  5466.       if (type == ISMACR)
  5467.     free (data);
  5468.       return;
  5469.     }
  5470.  
  5471.   keys = (char *)alloca (1 + (2 * strlen (keyseq)));
  5472.  
  5473.   /* Translate the ASCII representation of KEYSEQ into an array
  5474.      of characters.  Stuff the characters into ARRAY, and the
  5475.      length of ARRAY into LENGTH. */
  5476.   if (rl_translate_keyseq (keyseq, keys, &keys_len))
  5477.     return;
  5478.  
  5479.   /* Bind keys, making new keymaps as necessary. */
  5480.   for (i = 0; i < keys_len; i++)
  5481.     {
  5482.       if (i + 1 < keys_len)
  5483.     {
  5484.       if (map[keys[i]].type != ISKMAP)
  5485.         {
  5486.           if (map[i].type == ISMACR)
  5487.         free ((char *)map[i].function);
  5488.  
  5489.           map[keys[i]].type = ISKMAP;
  5490.           map[keys[i]].function = (Function *)rl_make_bare_keymap ();
  5491.         }
  5492.       map = (Keymap)map[keys[i]].function;
  5493.     }
  5494.       else
  5495.     {
  5496.       if (map[keys[i]].type == ISMACR)
  5497.         free ((char *)map[keys[i]].function);
  5498.  
  5499.       map[keys[i]].function = (Function *)data;
  5500.       map[keys[i]].type = type;
  5501.     }
  5502.     }
  5503. }
  5504.  
  5505. /* Translate the ASCII representation of SEQ, stuffing the
  5506.    values into ARRAY, an array of characters.  LEN gets the
  5507.    final length of ARRAY.  Return non-zero if there was an
  5508.    error parsing SEQ. */
  5509. rl_translate_keyseq (seq, array, len)
  5510.      char *seq, *array;
  5511.      int *len;
  5512. {
  5513.   register int i, c, l = 0;
  5514.  
  5515.   for (i = 0; c = seq[i]; i++)
  5516.     {
  5517.       if (c == '\\')
  5518.     {
  5519.       c = seq[++i];
  5520.  
  5521.       if (!c)
  5522.         break;
  5523.  
  5524.       if (((c == 'C' || c == 'M') &&  seq[i + 1] == '-') ||
  5525.           (c == 'e'))
  5526.         {
  5527.           /* Handle special case of backwards define. */
  5528.           if (strncmp (&seq[i], "C-\\M-", 5) == 0)
  5529.         {
  5530.           array[l++] = ESC;
  5531.           i += 5;
  5532.           array[l++] = CTRL (to_upper (seq[i]));
  5533.           if (!seq[i])
  5534.             i--;
  5535.           continue;
  5536.         }
  5537.  
  5538.           switch (c)
  5539.         {
  5540.         case 'M':
  5541.           i++;
  5542.           array[l++] = ESC;
  5543.           break;
  5544.  
  5545.         case 'C':
  5546.           i += 2;
  5547.           /* Special hack for C-?... */
  5548.           if (seq[i] == '?')
  5549.             array[l++] = RUBOUT;
  5550.           else
  5551.             array[l++] = CTRL (to_upper (seq[i]));
  5552.           break;
  5553.  
  5554.         case 'e':
  5555.           array[l++] = ESC;
  5556.         }
  5557.  
  5558.           continue;
  5559.         }
  5560.     }
  5561.       array[l++] = c;
  5562.     }
  5563.  
  5564.   *len = l;
  5565.   array[l] = '\0';
  5566.   return (0);
  5567. }
  5568.  
  5569. /* Return a pointer to the function that STRING represents.
  5570.    If STRING doesn't have a matching function, then a NULL pointer
  5571.    is returned. */
  5572. Function *
  5573. rl_named_function (string)
  5574.      char *string;
  5575. {
  5576.   register int i;
  5577.  
  5578.   for (i = 0; funmap[i]; i++)
  5579.     if (stricmp (funmap[i]->name, string) == 0)
  5580.       return (funmap[i]->function);
  5581.   return ((Function *)NULL);
  5582. }
  5583.  
  5584. /* The last key bindings file read. */
  5585. #ifdef __MSDOS__
  5586. /* Don't know what to do, but this is a guess */
  5587. static char *last_readline_init_file = "/INPUTRC";
  5588. #else
  5589. static char *last_readline_init_file = "~/inputrc";
  5590. #endif
  5591.  
  5592. /* Re-read the current keybindings file. */
  5593. rl_re_read_init_file (count, ignore)
  5594.      int count, ignore;
  5595. {
  5596.   rl_read_init_file ((char *)NULL);
  5597. }
  5598.  
  5599. /* Do key bindings from a file.  If FILENAME is NULL it defaults
  5600.    to `~/.inputrc'.  If the file existed and could be opened and
  5601.    read, 0 is returned, otherwise errno is returned. */
  5602. int
  5603. rl_read_init_file (filename)
  5604.      char *filename;
  5605. {
  5606.   register int i;
  5607.   char *buffer, *openname, *line, *end;
  5608.   struct stat finfo;
  5609.   int file;
  5610.  
  5611.   /* Default the filename. */
  5612.   if (!filename)
  5613.     filename = last_readline_init_file;
  5614.  
  5615.   openname = tilde_expand (filename);
  5616.  
  5617.   if (!openname || *openname == '\000')
  5618.     return ENOENT;
  5619.  
  5620.   if ((stat (openname, &finfo) < 0) ||
  5621.       (file = open (openname, O_RDONLY, 0666)) < 0)
  5622.     {
  5623.       free (openname);
  5624.       return (errno);
  5625.     }
  5626.   else
  5627.     free (openname);
  5628.  
  5629.   last_readline_init_file = filename;
  5630.  
  5631.   /* Read the file into BUFFER. */
  5632.   buffer = (char *)xmalloc (finfo.st_size + 1);
  5633.   i = read (file, buffer, finfo.st_size);
  5634.   close (file);
  5635.  
  5636.   if (i != finfo.st_size)
  5637.     return (errno);
  5638.  
  5639.   /* Loop over the lines in the file.  Lines that start with `#' are
  5640.      comments; all other lines are commands for readline initialization. */
  5641.   line = buffer;
  5642.   end = buffer + finfo.st_size;
  5643.   while (line < end)
  5644.     {
  5645.       /* Find the end of this line. */
  5646.       for (i = 0; line + i != end && line[i] != '\n'; i++);
  5647.  
  5648.       /* Mark end of line. */
  5649.       line[i] = '\0';
  5650.  
  5651.       /* If the line is not a comment, then parse it. */
  5652.       if (*line != '#')
  5653.     rl_parse_and_bind (line);
  5654.  
  5655.       /* Move to the next line. */
  5656.       line += i + 1;
  5657.     }
  5658.   return (0);
  5659. }
  5660.  
  5661. /* **************************************************************** */
  5662. /*                                    */
  5663. /*            Parser Directives                   */
  5664. /*                                    */
  5665. /* **************************************************************** */
  5666.  
  5667. /* Conditionals. */
  5668.  
  5669. /* Calling programs set this to have their argv[0]. */
  5670. char *rl_readline_name = "other";
  5671.  
  5672. /* Stack of previous values of parsing_conditionalized_out. */
  5673. static unsigned char *if_stack = (unsigned char *)NULL;
  5674. static int if_stack_depth = 0;
  5675. static int if_stack_size = 0;
  5676.  
  5677. /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
  5678. parser_if (args)
  5679.      char *args;
  5680. {
  5681.   register int i;
  5682.  
  5683.   /* Push parser state. */
  5684.   if (if_stack_depth + 1 >= if_stack_size)
  5685.     {
  5686.       if (!if_stack)
  5687.     if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
  5688.       else
  5689.     if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
  5690.     }
  5691.   if_stack[if_stack_depth++] = parsing_conditionalized_out;
  5692.  
  5693.   /* If parsing is turned off, then nothing can turn it back on except
  5694.      for finding the matching endif.  In that case, return right now. */
  5695.   if (parsing_conditionalized_out)
  5696.     return;
  5697.  
  5698.   /* Isolate first argument. */
  5699.   for (i = 0; args[i] && !whitespace (args[i]); i++);
  5700.  
  5701.   if (args[i])
  5702.     args[i++] = '\0';
  5703.  
  5704.   /* Handle "if term=foo" and "if mode=emacs" constructs.  If this
  5705.      isn't term=foo, or mode=emacs, then check to see if the first
  5706.      word in ARGS is the same as the value stored in rl_readline_name. */
  5707.   if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
  5708.     {
  5709.       char *tem, *tname;
  5710.  
  5711.       /* Terminals like "aaa-60" are equivalent to "aaa". */
  5712.       tname = savestring (rl_terminal_name);
  5713.       tem = rindex (tname, '-');
  5714.       if (tem)
  5715.     *tem = '\0';
  5716.  
  5717.       if (stricmp (args + 5, tname) == 0)
  5718.     parsing_conditionalized_out = 0;
  5719.       else
  5720.     parsing_conditionalized_out = 1;
  5721.     }
  5722. #if defined (VI_MODE)
  5723.   else if (strnicmp (args, "mode=", 5) == 0)
  5724.     {
  5725.       int mode;
  5726.  
  5727.       if (stricmp (args + 5, "emacs") == 0)
  5728.     mode = emacs_mode;
  5729.       else if (stricmp (args + 5, "vi") == 0)
  5730.     mode = vi_mode;
  5731.       else
  5732.     mode = no_mode;
  5733.  
  5734.       if (mode == rl_editing_mode)
  5735.     parsing_conditionalized_out = 0;
  5736.       else
  5737.     parsing_conditionalized_out = 1;
  5738.     }
  5739. #endif /* VI_MODE */
  5740.   /* Check to see if the first word in ARGS is the same as the
  5741.      value stored in rl_readline_name. */
  5742.   else if (stricmp (args, rl_readline_name) == 0)
  5743.     parsing_conditionalized_out = 0;
  5744.   else
  5745.     parsing_conditionalized_out = 1;
  5746. }
  5747.  
  5748. /* Invert the current parser state if there is anything on the stack. */
  5749. parser_else (args)
  5750.      char *args;
  5751. {
  5752.   register int i;
  5753.  
  5754.   if (!if_stack_depth)
  5755.     {
  5756.       /* Error message? */
  5757.       return;
  5758.     }
  5759.  
  5760.   /* Check the previous (n - 1) levels of the stack to make sure that
  5761.      we haven't previously turned off parsing. */
  5762.   for (i = 0; i < if_stack_depth - 1; i++)
  5763.     if (if_stack[i] == 1)
  5764.       return;
  5765.  
  5766.   /* Invert the state of parsing if at top level. */
  5767.   parsing_conditionalized_out = !parsing_conditionalized_out;
  5768. }
  5769.  
  5770. /* Terminate a conditional, popping the value of
  5771.    parsing_conditionalized_out from the stack. */
  5772. parser_endif (args)
  5773.      char *args;
  5774. {
  5775.   if (if_stack_depth)
  5776.     parsing_conditionalized_out = if_stack[--if_stack_depth];
  5777.   else
  5778.     {
  5779.       /* *** What, no error message? *** */
  5780.     }
  5781. }
  5782.  
  5783. /* Associate textual names with actual functions. */
  5784. static struct {
  5785.   char *name;
  5786.   Function *function;
  5787. } parser_directives [] = {
  5788.   { "if", parser_if },
  5789.   { "endif", parser_endif },
  5790.   { "else", parser_else },
  5791.   { (char *)0x0, (Function *)0x0 }
  5792. };
  5793.  
  5794. /* Handle a parser directive.  STATEMENT is the line of the directive
  5795.    without any leading `$'. */
  5796. static int
  5797. handle_parser_directive (statement)
  5798.      char *statement;
  5799. {
  5800.   register int i;
  5801.   char *directive, *args;
  5802.  
  5803.   /* Isolate the actual directive. */
  5804.  
  5805.   /* Skip whitespace. */
  5806.   for (i = 0; whitespace (statement[i]); i++);
  5807.  
  5808.   directive = &statement[i];
  5809.  
  5810.   for (; statement[i] && !whitespace (statement[i]); i++);
  5811.  
  5812.   if (statement[i])
  5813.     statement[i++] = '\0';
  5814.  
  5815.   for (; statement[i] && whitespace (statement[i]); i++);
  5816.  
  5817.   args = &statement[i];
  5818.  
  5819.   /* Lookup the command, and act on it. */
  5820.   for (i = 0; parser_directives[i].name; i++)
  5821.     if (stricmp (directive, parser_directives[i].name) == 0)
  5822.       {
  5823.     (*parser_directives[i].function) (args);
  5824.     return (0);
  5825.       }
  5826.  
  5827.   /* *** Should an error message be output? */
  5828.   return (1);
  5829. }
  5830.  
  5831. /* Ugly but working hack for binding prefix meta. */
  5832. #define PREFIX_META_HACK
  5833.  
  5834. static int substring_member_of_array ();
  5835.  
  5836. /* Read the binding command from STRING and perform it.
  5837.    A key binding command looks like: Keyname: function-name\0,
  5838.    a variable binding command looks like: set variable value.
  5839.    A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
  5840. rl_parse_and_bind (string)
  5841.      char *string;
  5842. {
  5843.   extern char *possible_control_prefixes[], *possible_meta_prefixes[];
  5844.   char *funname, *kname;
  5845.   register int c;
  5846.   int key, i;
  5847.  
  5848.   while (string && whitespace (*string))
  5849.     string++;
  5850.  
  5851.   if (!string || !*string || *string == '#')
  5852.     return;
  5853.  
  5854.   /* If this is a parser directive, act on it. */
  5855.   if (*string == '$')
  5856.     {
  5857.       handle_parser_directive (&string[1]);
  5858.       return;
  5859.     }
  5860.  
  5861.   /* If we are supposed to be skipping parsing right now, then do it. */
  5862.   if (parsing_conditionalized_out)
  5863.     return;
  5864.  
  5865.   i = 0;
  5866.   /* If this keyname is a complex key expression surrounded by quotes,
  5867.      advance to after the matching close quote. */
  5868.   if (*string == '"')
  5869.     {
  5870.       for (i = 1; c = string[i]; i++)
  5871.     {
  5872.       if (c == '"' && string[i - 1] != '\\')
  5873.         break;
  5874.     }
  5875.     }
  5876.  
  5877.   /* Advance to the colon (:) or whitespace which separates the two objects. */
  5878.   for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
  5879.  
  5880.   /* Mark the end of the command (or keyname). */
  5881.   if (string[i])
  5882.     string[i++] = '\0';
  5883.  
  5884.   /* If this is a command to set a variable, then do that. */
  5885.   if (stricmp (string, "set") == 0)
  5886.     {
  5887.       char *var = string + i;
  5888.       char *value;
  5889.  
  5890.       /* Make VAR point to start of variable name. */
  5891.       while (*var && whitespace (*var)) var++;
  5892.  
  5893.       /* Make value point to start of value string. */
  5894.       value = var;
  5895.       while (*value && !whitespace (*value)) value++;
  5896.       if (*value)
  5897.     *value++ = '\0';
  5898.       while (*value && whitespace (*value)) value++;
  5899.  
  5900.       rl_variable_bind (var, value);
  5901.       return;
  5902.     }
  5903.  
  5904.   /* Skip any whitespace between keyname and funname. */
  5905.   for (; string[i] && whitespace (string[i]); i++);
  5906.   funname = &string[i];
  5907.  
  5908.   /* Now isolate funname.
  5909.      For straight function names just look for whitespace, since
  5910.      that will signify the end of the string.  But this could be a
  5911.      macro definition.  In that case, the string is quoted, so skip
  5912.      to the matching delimiter. */
  5913.   if (*funname == '\'' || *funname == '"')
  5914.     {
  5915.       int delimiter = string[i++];
  5916.  
  5917.       for (; c = string[i]; i++)
  5918.     {
  5919.       if (c == delimiter && string[i - 1] != '\\')
  5920.         break;
  5921.     }
  5922.       if (c)
  5923.     i++;
  5924.     }
  5925.  
  5926.   /* Advance to the end of the string.  */
  5927.   for (; string[i] && !whitespace (string[i]); i++);
  5928.  
  5929.   /* No extra whitespace at the end of the string. */
  5930.   string[i] = '\0';
  5931.  
  5932.   /* If this is a new-style key-binding, then do the binding with
  5933.      rl_set_key ().  Otherwise, let the older code deal with it. */
  5934.   if (*string == '"')
  5935.     {
  5936.       char *seq = (char *)alloca (1 + strlen (string));
  5937.       register int j, k = 0;
  5938.  
  5939.       for (j = 1; string[j]; j++)
  5940.     {
  5941.       if (string[j] == '"' && string[j - 1] != '\\')
  5942.         break;
  5943.  
  5944.       seq[k++] = string[j];
  5945.     }
  5946.       seq[k] = '\0';
  5947.  
  5948.       /* Binding macro? */
  5949.       if (*funname == '\'' || *funname == '"')
  5950.     {
  5951.       j = strlen (funname);
  5952.  
  5953.       if (j && funname[j - 1] == *funname)
  5954.         funname[j - 1] = '\0';
  5955.  
  5956.       rl_macro_bind (seq, &funname[1], keymap);
  5957.     }
  5958.       else
  5959.     rl_set_key (seq, rl_named_function (funname), keymap);
  5960.  
  5961.       return;
  5962.     }
  5963.  
  5964.   /* Get the actual character we want to deal with. */
  5965.   kname = rindex (string, '-');
  5966.   if (!kname)
  5967.     kname = string;
  5968.   else
  5969.     kname++;
  5970.  
  5971.   key = glean_key_from_name (kname);
  5972.  
  5973.   /* Add in control and meta bits. */
  5974.   if (substring_member_of_array (string, possible_control_prefixes))
  5975.     key = CTRL (to_upper (key));
  5976.  
  5977.   if (substring_member_of_array (string, possible_meta_prefixes))
  5978.     key = META (key);
  5979.  
  5980.   /* Temporary.  Handle old-style keyname with macro-binding. */
  5981.   if (*funname == '\'' || *funname == '"')
  5982.     {
  5983.       char seq[2];
  5984.       int fl = strlen (funname);
  5985.  
  5986.       seq[0] = key; seq[1] = '\0';
  5987.       if (fl && funname[fl - 1] == *funname)
  5988.     funname[fl - 1] = '\0';
  5989.  
  5990.       rl_macro_bind (seq, &funname[1], keymap);
  5991.     }
  5992. #if defined (PREFIX_META_HACK)
  5993.   /* Ugly, but working hack to keep prefix-meta around. */
  5994.   else if (stricmp (funname, "prefix-meta") == 0)
  5995.     {
  5996.       char seq[2];
  5997.  
  5998.       seq[0] = key;
  5999.       seq[1] = '\0';
  6000.       rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, keymap);
  6001.     }
  6002. #endif /* PREFIX_META_HACK */
  6003.   else
  6004.     rl_bind_key (key, rl_named_function (funname));
  6005. }
  6006.  
  6007. rl_variable_bind (name, value)
  6008.      char *name, *value;
  6009. {
  6010.   if (stricmp (name, "editing-mode") == 0)
  6011.     {
  6012.       if (strnicmp (value, "vi", 2) == 0)
  6013.     {
  6014. #if defined (VI_MODE)
  6015.       keymap = vi_insertion_keymap;
  6016.       rl_editing_mode = vi_mode;
  6017. #else
  6018. #if defined (NOTDEF)
  6019.       /* What state is the terminal in?  I'll tell you:
  6020.          non-determinate!  That means we cannot do any output. */
  6021.       ding ();
  6022. #endif /* NOTDEF */
  6023. #endif /* VI_MODE */
  6024.     }
  6025.       else if (strnicmp (value, "emacs", 5) == 0)
  6026.     {
  6027.       keymap = emacs_standard_keymap;
  6028.       rl_editing_mode = emacs_mode;
  6029.     }
  6030.     }
  6031.   else if (stricmp (name, "horizontal-scroll-mode") == 0)
  6032.     {
  6033.       if (!*value || stricmp (value, "On") == 0)
  6034.     horizontal_scroll_mode = 1;
  6035.       else
  6036.     horizontal_scroll_mode = 0;
  6037.     }
  6038.   else if (stricmp (name, "mark-modified-lines") == 0)
  6039.     {
  6040.       if (!*value || stricmp (value, "On") == 0)
  6041.     mark_modified_lines = 1;
  6042.       else
  6043.     mark_modified_lines = 0;
  6044.     }
  6045.   else if (stricmp (name, "prefer-visible-bell") == 0)
  6046.     {
  6047.       if (!*value || stricmp (value, "On") == 0)
  6048.         prefer_visible_bell = 1;
  6049.       else
  6050.         prefer_visible_bell = 0;
  6051.     }
  6052.   else if (stricmp (name, "comment-begin") == 0)
  6053.     {
  6054. #if defined (VI_MODE)
  6055.       extern char *rl_vi_comment_begin;
  6056.  
  6057.       if (*value)
  6058.     {
  6059.       if (rl_vi_comment_begin)
  6060.         free (rl_vi_comment_begin);
  6061.  
  6062.       rl_vi_comment_begin = savestring (value);
  6063.     }
  6064. #endif /* VI_MODE */
  6065.     }
  6066. }
  6067.  
  6068. /* Return the character which matches NAME.
  6069.    For example, `Space' returns ' '. */
  6070.  
  6071. typedef struct {
  6072.   char *name;
  6073.   int value;
  6074. } assoc_list;
  6075.  
  6076. assoc_list name_key_alist[] = {
  6077.   { "DEL", 0x7f },
  6078.   { "ESC", '\033' },
  6079.   { "Escape", '\033' },
  6080.   { "LFD", '\n' },
  6081.   { "Newline", '\n' },
  6082.   { "RET", '\r' },
  6083.   { "Return", '\r' },
  6084.   { "Rubout", 0x7f },
  6085.   { "SPC", ' ' },
  6086.   { "Space", ' ' },
  6087.   { "Tab", 0x09 },
  6088.   { (char *)0x0, 0 }
  6089. };
  6090.  
  6091. int
  6092. glean_key_from_name (name)
  6093.      char *name;
  6094. {
  6095.   register int i;
  6096.  
  6097.   for (i = 0; name_key_alist[i].name; i++)
  6098.     if (stricmp (name, name_key_alist[i].name) == 0)
  6099.       return (name_key_alist[i].value);
  6100.  
  6101.   return (*name);
  6102. }
  6103.  
  6104.  
  6105. /* **************************************************************** */
  6106. /*                                    */
  6107. /*          Key Binding and Function Information            */
  6108. /*                                    */
  6109. /* **************************************************************** */
  6110.  
  6111. /* Each of the following functions produces information about the
  6112.    state of keybindings and functions known to Readline.  The info
  6113.    is always printed to rl_outstream, and in such a way that it can
  6114.    be read back in (i.e., passed to rl_parse_and_bind (). */
  6115.  
  6116. /* Print the names of functions known to Readline. */
  6117. void
  6118. rl_list_funmap_names (ignore)
  6119.      int ignore;
  6120. {
  6121.   register int i;
  6122.   char **funmap_names;
  6123.   extern char **rl_funmap_names ();
  6124.  
  6125.   funmap_names = rl_funmap_names ();
  6126.  
  6127.   if (!funmap_names)
  6128.     return;
  6129.  
  6130.   for (i = 0; funmap_names[i]; i++)
  6131.     fprintf (rl_outstream, "%s\n", funmap_names[i]);
  6132.  
  6133.   free (funmap_names);
  6134. }
  6135.  
  6136. /* Return a NULL terminated array of strings which represent the key
  6137.    sequences that are used to invoke FUNCTION in MAP. */
  6138. static char **
  6139. invoking_keyseqs_in_map (function, map)
  6140.      Function *function;
  6141.      Keymap map;
  6142. {
  6143.   register int key;
  6144.   char **result;
  6145.   int result_index, result_size;
  6146.  
  6147.   result = (char **)NULL;
  6148.   result_index = result_size = 0;
  6149.  
  6150.   for (key = 0; key < 128; key++)
  6151.     {
  6152.       switch (map[key].type)
  6153.     {
  6154.     case ISMACR:
  6155.       /* Macros match, if, and only if, the pointers are identical.
  6156.          Thus, they are treated exactly like functions in here. */
  6157.     case ISFUNC:
  6158.       /* If the function in the keymap is the one we are looking for,
  6159.          then add the current KEY to the list of invoking keys. */
  6160.       if (map[key].function == function)
  6161.         {
  6162.           char *keyname = (char *)xmalloc (5);
  6163.  
  6164.           if (CTRL_P (key))
  6165.         sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
  6166.           else if (key == RUBOUT)
  6167.         sprintf (keyname, "\\C-?");
  6168.           else
  6169.         sprintf (keyname, "%c", key);
  6170.           
  6171.           if (result_index + 2 > result_size)
  6172.         {
  6173.           if (!result)
  6174.             result = (char **) xmalloc
  6175.               ((result_size = 10) * sizeof (char *));
  6176.           else
  6177.             result = (char **) xrealloc
  6178.               (result, (result_size += 10) * sizeof (char *));
  6179.         }
  6180.  
  6181.           result[result_index++] = keyname;
  6182.           result[result_index] = (char *)NULL;
  6183.         }
  6184.       break;
  6185.  
  6186.     case ISKMAP:
  6187.       {
  6188.         char **seqs = (char **)NULL;
  6189.  
  6190.         /* Find the list of keyseqs in this map which have FUNCTION as
  6191.            their target.  Add the key sequences found to RESULT. */
  6192.         if (map[key].function)
  6193.           seqs =
  6194.         invoking_keyseqs_in_map (function, (Keymap)map[key].function);
  6195.  
  6196.         if (seqs)
  6197.           {
  6198.         register int i;
  6199.  
  6200.         for (i = 0; seqs[i]; i++)
  6201.           {
  6202.             char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
  6203.  
  6204.             if (key == ESC)
  6205.               sprintf (keyname, "\\e");
  6206.             else if (CTRL_P (key))
  6207.               sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
  6208.             else if (key == RUBOUT)
  6209.               sprintf (keyname, "\\C-?");
  6210.             else
  6211.               sprintf (keyname, "%c", key);
  6212.  
  6213.             strcat (keyname, seqs[i]);
  6214.  
  6215.             if (result_index + 2 > result_size)
  6216.               {
  6217.             if (!result)
  6218.               result = (char **)
  6219.                 xmalloc ((result_size = 10) * sizeof (char *));
  6220.             else
  6221.               result = (char **)
  6222.                 xrealloc (result,
  6223.                       (result_size += 10) * sizeof (char *));
  6224.               }
  6225.  
  6226.             result[result_index++] = keyname;
  6227.             result[result_index] = (char *)NULL;
  6228.           }
  6229.           }
  6230.       }
  6231.       break;
  6232.     }
  6233.     }
  6234.   return (result);
  6235. }
  6236.  
  6237. /* Return a NULL terminated array of strings which represent the key
  6238.    sequences that can be used to invoke FUNCTION using the current keymap. */
  6239. char **
  6240. rl_invoking_keyseqs (function)
  6241.      Function *function;
  6242. {
  6243.   return (invoking_keyseqs_in_map (function, keymap));
  6244. }
  6245.  
  6246. /* Print all of the current functions and their bindings to
  6247.    rl_outstream.  If an explicit argument is given, then print
  6248.    the output in such a way that it can be read back in. */
  6249. int
  6250. rl_dump_functions (count)
  6251.      int count;
  6252. {
  6253.   void rl_function_dumper ();
  6254.  
  6255.   rl_function_dumper (rl_explicit_arg);
  6256.   rl_on_new_line ();
  6257.   return (0);
  6258. }
  6259.  
  6260. /* Print all of the functions and their bindings to rl_outstream.  If
  6261.    PRINT_READABLY is non-zero, then print the output in such a way
  6262.    that it can be read back in. */
  6263. void
  6264. rl_function_dumper (print_readably)
  6265.      int print_readably;
  6266. {
  6267.   register int i;
  6268.   char **rl_funmap_names (), **names;
  6269.   char *name;
  6270.  
  6271.   names = rl_funmap_names ();
  6272.  
  6273.   fprintf (rl_outstream, "\n");
  6274.  
  6275.   for (i = 0; name = names[i]; i++)
  6276.     {
  6277.       Function *function;
  6278.       char **invokers;
  6279.  
  6280.       function = rl_named_function (name);
  6281.       invokers = invoking_keyseqs_in_map (function, keymap);
  6282.  
  6283.       if (print_readably)
  6284.     {
  6285.       if (!invokers)
  6286.         fprintf (rl_outstream, "# %s (not bound)\n", name);
  6287.       else
  6288.         {
  6289.           register int j;
  6290.  
  6291.           for (j = 0; invokers[j]; j++)
  6292.         {
  6293.           fprintf (rl_outstream, "\"%s\": %s\n",
  6294.                invokers[j], name);
  6295.           free (invokers[j]);
  6296.         }
  6297.  
  6298.           free (invokers);
  6299.         }
  6300.     }
  6301.       else
  6302.     {
  6303.       if (!invokers)
  6304.         fprintf (rl_outstream, "%s is not bound to any keys\n",
  6305.              name);
  6306.       else
  6307.         {
  6308.           register int j;
  6309.  
  6310.           fprintf (rl_outstream, "%s can be found on ", name);
  6311.  
  6312.           for (j = 0; invokers[j] && j < 5; j++)
  6313.         {
  6314.           fprintf (rl_outstream, "\"%s\"%s", invokers[j],
  6315.                invokers[j + 1] ? ", " : ".\n");
  6316.         }
  6317.  
  6318.           if (j == 5 && invokers[j])
  6319.         fprintf (rl_outstream, "...\n");
  6320.  
  6321.           for (j = 0; invokers[j]; j++)
  6322.         free (invokers[j]);
  6323.  
  6324.           free (invokers);
  6325.         }
  6326.     }
  6327.     }
  6328. }
  6329.  
  6330.  
  6331. /* **************************************************************** */
  6332. /*                                    */
  6333. /*            String Utility Functions            */
  6334. /*                                    */
  6335. /* **************************************************************** */
  6336.  
  6337. static char *strindex ();
  6338.  
  6339. /* Return non-zero if any members of ARRAY are a substring in STRING. */
  6340. static int
  6341. substring_member_of_array (string, array)
  6342.      char *string, **array;
  6343. {
  6344.   while (*array)
  6345.     {
  6346.       if (strindex (string, *array))
  6347.     return (1);
  6348.       array++;
  6349.     }
  6350.   return (0);
  6351. }
  6352.  
  6353. /* Whoops, Unix doesn't have strnicmp. */
  6354.  
  6355. /* Compare at most COUNT characters from string1 to string2.  Case
  6356.    doesn't matter. */
  6357. static int
  6358. strnicmp (string1, string2, count)
  6359.      char *string1, *string2;
  6360. {
  6361.   register char ch1, ch2;
  6362.  
  6363.   while (count)
  6364.     {
  6365.       ch1 = *string1++;
  6366.       ch2 = *string2++;
  6367.       if (to_upper(ch1) == to_upper(ch2))
  6368.     count--;
  6369.       else break;
  6370.     }
  6371.   return (count);
  6372. }
  6373.  
  6374. /* strcmp (), but caseless. */
  6375. static int
  6376. stricmp (string1, string2)
  6377.      char *string1, *string2;
  6378. {
  6379.   register char ch1, ch2;
  6380.  
  6381.   while (*string1 && *string2)
  6382.     {
  6383.       ch1 = *string1++;
  6384.       ch2 = *string2++;
  6385.       if (to_upper(ch1) != to_upper(ch2))
  6386.     return (1);
  6387.     }
  6388.   return (*string1 | *string2);
  6389. }
  6390.  
  6391. /* Determine if s2 occurs in s1.  If so, return a pointer to the
  6392.    match in s1.  The compare is case insensitive. */
  6393. static char *
  6394. strindex (s1, s2)
  6395.      register char *s1, *s2;
  6396. {
  6397.   register int i, l = strlen (s2);
  6398.   register int len = strlen (s1);
  6399.  
  6400.   for (i = 0; (len - i) >= l; i++)
  6401.     if (strnicmp (&s1[i], s2, l) == 0)
  6402.       return (s1 + i);
  6403.   return ((char *)NULL);
  6404. }
  6405.  
  6406.  
  6407. /* **************************************************************** */
  6408. /*                                    */
  6409. /*            USG (System V) Support                */
  6410. /*                                    */
  6411. /* **************************************************************** */
  6412.  
  6413. /* When compiling and running in the `Posix' environment, Ultrix does
  6414.    not restart system calls, so this needs to do it. */
  6415. int
  6416. rl_getc (stream)
  6417.      FILE *stream;
  6418. {
  6419.   int result;
  6420.   unsigned char c;
  6421.  
  6422. #ifdef __GO32__
  6423.   if (isatty(0))
  6424.     return getkey();
  6425. #endif /* __GO32__ */
  6426.  
  6427.   while (1)
  6428.     {
  6429.       result = read (fileno (stream), &c, sizeof (char));
  6430.  
  6431.       if (result == sizeof (char))
  6432.     return (c);
  6433.  
  6434.       /* If zero characters are returned, then the file that we are
  6435.      reading from is empty!  Return EOF in that case. */
  6436.       if (result == 0)
  6437.     return (EOF);
  6438.  
  6439. #ifndef __GO32__
  6440.       /* If the error that we received was SIGINT, then try again,
  6441.      this is simply an interrupted system call to read ().
  6442.      Otherwise, some error ocurred, also signifying EOF. */
  6443.       if (errno != EINTR)
  6444.     return (EOF);
  6445. #endif /* !__GO32__ */
  6446.     }
  6447. }
  6448.  
  6449. #if defined (STATIC_MALLOC)
  6450.  
  6451. /* **************************************************************** */
  6452. /*                                    */
  6453. /*            xmalloc and xrealloc ()                     */
  6454. /*                                    */
  6455. /* **************************************************************** */
  6456.  
  6457. static void memory_error_and_abort ();
  6458.  
  6459. static char *
  6460. xmalloc (bytes)
  6461.      int bytes;
  6462. {
  6463.   char *temp = (char *)malloc (bytes);
  6464.  
  6465.   if (!temp)
  6466.     memory_error_and_abort ();
  6467.   return (temp);
  6468. }
  6469.  
  6470. static char *
  6471. xrealloc (pointer, bytes)
  6472.      char *pointer;
  6473.      int bytes;
  6474. {
  6475.   char *temp;
  6476.  
  6477.   if (!pointer)
  6478.     temp = (char *)malloc (bytes);
  6479.   else
  6480.     temp = (char *)realloc (pointer, bytes);
  6481.  
  6482.   if (!temp)
  6483.     memory_error_and_abort ();
  6484.  
  6485.   return (temp);
  6486. }
  6487.  
  6488. static void
  6489. memory_error_and_abort ()
  6490. {
  6491.   fprintf (stderr, "readline: Out of virtual memory!\n");
  6492.   abort ();
  6493. }
  6494. #endif /* STATIC_MALLOC */
  6495.  
  6496.  
  6497. /* **************************************************************** */
  6498. /*                                    */
  6499. /*            Testing Readline                */
  6500. /*                                    */
  6501. /* **************************************************************** */
  6502.  
  6503. #if defined (TEST)
  6504.  
  6505. main ()
  6506. {
  6507.   HIST_ENTRY **history_list ();
  6508.   char *temp = (char *)NULL;
  6509.   char *prompt = "readline% ";
  6510.   int done = 0;
  6511.  
  6512.   while (!done)
  6513.     {
  6514.       temp = readline (prompt);
  6515.  
  6516.       /* Test for EOF. */
  6517.       if (!temp)
  6518.     exit (1);
  6519.  
  6520.       /* If there is anything on the line, print it and remember it. */
  6521.       if (*temp)
  6522.     {
  6523.       fprintf (stderr, "%s\r\n", temp);
  6524.       add_history (temp);
  6525.     }
  6526.  
  6527.       /* Check for `command' that we handle. */
  6528.       if (strcmp (temp, "quit") == 0)
  6529.     done = 1;
  6530.  
  6531.       if (strcmp (temp, "list") == 0)
  6532.     {
  6533.       HIST_ENTRY **list = history_list ();
  6534.       register int i;
  6535.       if (list)
  6536.         {
  6537.           for (i = 0; list[i]; i++)
  6538.         {
  6539.           fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
  6540.           free (list[i]->line);
  6541.         }
  6542.           free (list);
  6543.         }
  6544.     }
  6545.       free (temp);
  6546.     }
  6547. }
  6548.  
  6549. #endif /* TEST */
  6550.  
  6551.  
  6552. /*
  6553.  * Local variables:
  6554.  * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"
  6555.  * end:
  6556.  */
  6557. @
  6558.